0

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.

Mina Hafzalla
  • 2,681
  • 9
  • 30
  • 44
  • 1
    To print only the shortened url use echo $return['url']; – maxhb Dec 05 '15 at 19:57
  • Possible duplicate of [Accessing associative arrays in PHP](http://stackoverflow.com/questions/3842111/accessing-associative-arrays-in-php) – cyfur01 Dec 05 '15 at 20:07

1 Answers1

1

maybe this :

<input type="text" value="<?php echo $return['data']['url'] ?>" />
thepiyush13
  • 1,321
  • 1
  • 8
  • 9