0

I'm trying to shorten URL using the bit.ly API V4, but i always get the same error, I can't get the short URL. Can someone help me please? Here is the code that Im using:

$long_url = 'https://estaticos.muyinteresante.es/media/cache/1140x_thumb/uploads/images/gallery/59bbb29c5bafe878503c9872/husky-siberiano-bosque.jpg';
$apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks';
$genericAccessToken = 'MyToken';

$data = array(
    'long_url' => $long_url
);
$payload = json_encode($data);

$header = array(
    'Authorization: Bearer ' . $genericAccessToken,
    'Content-Type: application/json',
    'Content-Length: ' . strlen($payload)
);

$ch = curl_init($apiv4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
$resultToJson = json_decode($result);

if (isset($resultToJson->link)) {
    echo $resultToJson->link;
}
else {
    echo 'Not found';
}

I always get "Not found", anybody knows why?

  • 1
    I've just copied and pasted your code, changed the access token and run the script. It works 100% without issues. Is it possible that the `not found` error is because you're not calling your own script correctly? Where do you save this script and how do you access it? – Andrea Olivato Jun 05 '21 at 04:32

1 Answers1

0

I have a very detailed answer on integrating with Bit.ly V4 and how to shorten URLs.

You can find the link here.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Andrew Reese
  • 854
  • 1
  • 11
  • 27