1
<?php
$access_token =  '************* MY BITLY SECRET TOKEN HERE ************';

$long_url='http://example.com/calculator?TokenA=APPLE&PriceA=5.061&TokenB=ORANGE&PriceB=3.02&QuantityB=90.44&PriceAend=7.76&PriceBend=2.287&RewardPrice=2.2&RewardQuantity=14.88&submit=SHOW+ME+MONEY';

$url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$access_token.'&longUrl='.$long_url;
$ch1 = curl_init($url);  //open connection
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "GET"); 
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec($ch1); //execute post
$data_array = json_decode($result);
echo "<pre>";
print_r($newdata);
echo 'Long url:  '.$long_url.'<br>';
echo 'shortened url: '.$data_array->data->url;
?>

I am very new to PHP. I found the codes above in a tutorial.

I am trying to use the Bitly API to generate shortened URL. I am just one step away from success. Bitly doesn't seem to recognize the "&" characters in URL and truncated the URL there.

When I clicked on the shortened URL, it went to:

http://example.com/calculator?TokenA=APPLE

I need the Bitly URL to point to the complete long URL. And because of this error, the calculator page failed to show the proper result. It seems that something is not right with the "&" character.

Lawrence L.
  • 69
  • 1
  • 7
  • 1
    Looks like you need to `urlencode($long_url)` so that it stays a singular variable in the whole url. If you `echo $url;` you will see how the extra &s look like extra GET params. – IncredibleHat Aug 10 '21 at 16:31
  • Are you sure it's `longUrl` and not `long_url`? The documentation makes it look like it should be the latter. – Robson Aug 10 '21 at 16:32

0 Answers0