简体   繁体   中英

bit.ly dynamic link php

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.

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM