我正在使用bit.ly api尝试一次缩短多个网址,但没有取得任何成功。
<?php
$sites = array(
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl.com',
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl3.com',
);
foreach ( $sites as $site ) {
$xmlDoc = new DOMDocument();
if ( @$xmlDoc->load($site) ) {
echo "{$site} successful<br />\n";
} else {
echo "{$site} invalid<br />\n";
}
}
die();
?>
如何将长网址数组转换为bit.ly列表?
答案 0 :(得分:2)
尝试使用file_get_contents()
$sites = array(
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl.com',
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl3.com',
);
foreach ( $sites as $site ) {
$shortened_url = file_get_contents($site);
if($shortened_url)
echo "$shortened_url <br/>";
}
die();
答案 1 :(得分:1)
这是一个生成链接的函数,请务必用你的命令替换登录名和api密钥:
function get_short_link($url) {
$bitly_login="**login**";
$bitly_apikey="**apikey**";
$api_call = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$bitly_login."&apiKey=".$bitly_apikey);
$bitlyinfo=json_decode(utf8_encode($api_call),true);
if ($bitlyinfo['errorCode']==0) {
return $bitlyinfo['results'][urldecode($url)]['shortUrl'];
} else {
return false;
}
}
然后,运行该函数将其抛入foreach循环:
$bitlyarray = array();
$arraylist = array(
'http://www.siteone.com',
'http://www.sitetwo.com',
'http://www.sitethree.com'
);
foreach($arraylist as $link)
{
$bitlyarray[] = get_short_link($link);
}
var_dump($bitlyarray);
希望有所帮助。
答案 2 :(得分:1)
这是一个与bit.ly API集成的免费PHP客户端库: https://www.mashape.com/mashaper/bitly
将getShortenedUrl挂钩到你的foreach循环中,你应该是金色的。
完全披露,我是Mashape的Craftsman Advocate,也是我上大学以来的PHP黑客。如果您有任何其他问题,请随时与我联系。
答案 3 :(得分:0)
就我个人而言,我会使用curl over fgc更快,因为你想要批量转换,那么为什么不实现curl multi,这将加快api请求的等待时间。更快更好吗?
function curl_multi($urls) {
$curly = array();
$result = array();
$mh = curl_multi_init();
foreach ($urls as $id=>$url) {
$curly[$id] = curl_init();
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curly[$id], CURLOPT_TIMEOUT, 30);
curl_setopt($curly[$id], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
curl_setopt($curly[$id], CURLOPT_REFERER, $url);
curl_setopt($curly[$id], CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curly[$id], CURLOPT_AUTOREFERER, true);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $curly[$id]);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
curl_multi_close($mh);
return $result;
}
$sites = array(
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl.com',
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl3.com',
);
$result = curl_multi($sites);
print_r($result);
/*
Array
(
[0] => http://bit.ly/UhaW9E
[1] => http://bit.ly/RlX21s
)
*/
答案 4 :(得分:0)