就我个人而言,我会在 fgc 上使用 curl,因为它更快,并且由于您想要进行批量转换,那么为什么不实现 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
)
*/