我在这个网站上找到了下面的 Bitly API 代码。我很难让它创建并回显一个名为 $fullurl 的变量的位缩短 URL。我该怎么做?
编辑:没有出现错误代码,只是没有显示缩短的 URL。
编辑 2: var_dump($response);
返回 NULL
编辑 3:我确实用我的替换了 API 登录名和密钥。
编辑 4:我在原始教程的评论之一中找到了答案。对于所有 PHP 专业人士来说,我的问题太基本了:我只需要echo bitly_shorten($fullurl);
在最后添加。
提前致谢,
约翰
function bitly_shorten($url)
{
$query = array(
"version" => "2.0.1",
"longUrl" => $url,
"login" => API_LOGIN, // replace with your login
"apiKey" => API_KEY // replace with your api key
);
$query = http_build_query($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.bit.ly/shorten?".$query);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->errorCode == 0 && $response->statusCode == "OK") {
return $response->results->{$url}->shortUrl;
} else {
return null;
}
}