使用 Bitly API 缩短 URL
我在 此 网站上找到了下面的 Bitly API 代码。我很难让它创建并回显名为 $fullurl 的变量的稍微缩短的 URL。我该怎么做呢?
编辑: 没有出现错误代码,只是没有显示稍微缩短的 URL。
编辑 2: var_dump($response);
返回 NULL
编辑 3: 我确实用我的 API 登录名和密钥替换了 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;
}
}
I found the Bitly API code below on this site. I'm having a hard time getting it to create and then echo a Bitly shortened URL for a variable called $fullurl. How would I do that?
EDIT: No error code appears, just no bitly shortened URL is shown.
EDIT 2: var_dump($response);
returns NULL
EDIT 3: I did replace the API login and key with my mine.
EDIT 4: I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add echo bitly_shorten($fullurl);
at the end.
Thanks in advance,
John
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其更改为:
Change it to:
bit.ly 似乎更新了他们的 api ,请访问
http:// code.google.com/p/bitly-api/wiki/ApiDocumentation#Authentication_and_Shared_Parameters
for api..
url 似乎是这样的,
http://api.bitly.com/v3/shorten?.....
他们所说的新版本是 3,在您的代码中是 2.0.1
每当您使用在线服务的 api 时,最好从他们的网站获取它,而不是从任何第三方网站或博客获取。
It seems that bit.ly had updated their api , please visit
http://code.google.com/p/bitly-api/wiki/ApiDocumentation#Authentication_and_Shared_Parameters
for api..
The url seems to be something like this,
http://api.bitly.com/v3/shorten?.....
The new version they stated is 3 and in your code its 2.0.1
Whenever you are using an api of an online service its better to get it from their site instead of getting that from any third party site or blog..
我在原始教程的评论之一中找到了答案。对于所有 PHP 专业人士来说,我的问题太基础了:我只需要在末尾添加
echo bitly_shorten($fullurl);
即可。I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add
echo bitly_shorten($fullurl);
at the end.