bit.ly动态链接php

时间:2012-10-13 02:36:49

标签: php variables dynamic hyperlink bit.ly

我想在我的网站上使用bit.ly。当我这样做时:

$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;

工作正常。

但是当我尝试将链接放在变量中时。

$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;

它不起作用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

检查file_get_contents()的输出是什么。你可以这样做:

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

如果那里有良好的输出,请检查并将其传递给json_encode()