$ip='218.202.4.135';
$city = music_ip_city($ip);
echo $city;
function music_ip_city($ip){

$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$ip}";
$ctx=stream_context_create(array(
'http'=>array(
        'timeout'=>10//等待10秒
        )
    )
);
$y =file_get_contents($url,0,$ctx);
if($y){
$message = json_decode($y,true);
$res = $message['city'];
}else{
$res = "北京";
}

//echo $res; return $res;
}Warning: file_get_contents(http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.202.4.135) [function.file-get-contents]: failed to open stream: HTTP request failed! in 

解决方案 »

  1.   

    代码没错吧。用file_get_contents请求时。注意空格之类的。要base64_encode一下
      

  2.   

    代码是没有错误的,出现警告是当时请求http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$ip}该网址时没有链接上,当时这个网址出错了
      

  3.   

    出现警告是因为在指定时间内未完成获取内容。
    在file_get_contents前面加@屏蔽错误。
    $y =@file_get_contents($url,0,$ctx);
      

  4.   

    在file_get_contents方法前面加一个@,就行了