cURL导致页面挂起

时间:2016-04-27 23:41:25

标签: php curl

所以我有这个功能来抓住目标拉链30英里内的邮政拉链。

我在我的网页中使用了这个功能,但后来我意识到页面无法完成加载。一旦它加载,在新标签中打开网址也不会有效,我必须重新启动整个浏览器才能“恢复”它。

经过很长时间的调试后,我发现罪魁祸首就是这个功能。 此函数在同一参数上不时挂起(永远响应并返回结果)。

//Get nearby postal zip by using geoname api
//genoname api provide both xml and json version
function getNearByPostalZip($zip){

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://api.geonames.org/findNearbyPostalCodesJSON?postalcode=".$zip."&country=US&radius=30&username=xxxx&maxRows=100"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
// $output contains the output string 
$output = curl_exec($ch); 
curl_close($ch);   

$temp = json_decode($output);
$zips = array();
foreach($temp->postalCodes as $key=>$postalcode)
{
    $zips[] = array('zip'=>$postalcode->postalCode,'distance'=>$postalcode->distance);
}
return $zips;
}

0 个答案:

没有答案