我在this网站上找到了下面的bitly api代码。我很难让它创建,然后为一个名为$fullurl的变量回显一个稍微缩短的url。我该怎么做?
编辑:没有出现错误代码,只是没有显示位缩短的url。
编辑2:var_dump($response);
返回空值
编辑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;
}
}
最佳答案
更改为:
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.bitly.com/v3/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->status_txt == "OK") {
return $response->data->url;
} else {
return null;
}
}
关于php - 使用Bitly API缩短URL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5799392/