我想通过php脚本获取yahoo woeid。
我可以访问 url 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="NewYork"&format=json' 并在我的中获取 json 文件浏览器。但是当我使用curl时它返回null。
这就是代码。
public function getwoeid(){
$ch = curl_init();
$url = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'.$this->cityname.'"&format=json';
//echo $url;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data=curl_exec($ch) or die($this->returnerror());
curl_close($ch);
$array = json_decode($data, true);
return $array;
}
当我尝试访问 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="上海"&format=json' 时,curl 工作正常。
我无法修复它。
如有任何建议,我们将不胜感激! 谢谢。
最佳答案
你可以只使用 PHP 的 file_get_contents() 。空格必须由 urlencode() 编码;
$url = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'.$this->cityname.'"&format=json';
$data = file_get_contents(urlencode($url));
$array = json_decode($data, true);
关于php - 为什么curl返回null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12212084/