Question

I am trying to use bit.ly in my website. When I do this:

$short_url = json_decode(file_get_contents("http://api.bit.ly/v3/shorten?login=myLogin&apiKey=myApiKey&longUrl=".urlencode("http://example.com")."&format=json"))->data->url;

It works fine.

However when I try placing the link in a variable.

$link = 'http://example.com';
$short_url = json_decode(file_get_contents("http://api.bit.ly/v3/shorten?login=myLogin&apiKey=myApiKey&longUrl=".urlencode(**$link**)."&format=json"))->data->url;

It does not work. Any help would be appreciated.

Was it helpful?

Solution

Check what is the output of file_get_contents(). You can do it this way:

var_dump(file_get_contents("http://api.bit.ly/v3/shorten?login=myLogin&apiKey=myApiKey&longUrl=".urlencode($link)."&format=json"));

If there's a good output there, check and then pass it to json_encode().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top