TAGS :Viewed: 5 - Published at: a few seconds ago

[ How to post a Bitlink shortened URL to input value ]

Below is the PHP code I'm using for shortening long URLs via Bitly API:

<?php
$bitly_access_token = 'my_api_key';
$deeplink = 'http://example.com/';

$curl = curl_init('https://api-ssl.bit.ly/v3/shorten?access_token='.$bitly_access_token.'&longUrl='.urlencode($deeplink));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
$return = json_decode(curl_exec($curl), true);
curl_close($curl);

print_r($return);
?>

A sample output of the above code:

Array ( [status_code] => 200 [status_txt] => OK [data] => Array ( [long_url] => http://example.com [url] => http://bit.ly/xxxxx [hash] => xxxxx [global_hash] => zzzzz [new_hash] => 0 ) )

It works and output the shortened url as: http://bit.ly/xxxxx (Sample).

But, with a little experience in PHP, I can't figure out how to post that shortened URL into an input value. I tried <input type="text" value="<?php echo $return ?>" /> but that didn't work.

Also, I have problem with this long output, why it isn't displaying the shortened URL only?

Thank you.

Answer 1


maybe this :

<input type="text" value="<?php echo $return['data']['url'] ?>" />