php - 为什么curl返回null?

标签 php curl

我想通过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/

相关文章:

php - 根据下拉列表中选定的值填充 '<input>' 值

php - Firebase 云消息传递 : Add a device to a topic using curl command

php - 拉拉维尔 : efficient way to count related models with where clause

php - 生成 PHPUnit 报告(失败和通过的测试)

php - 如何在我的管理页面中显示数据库

php - 搜索一张表时的多个条件

c++ - CURL 执行完毕超时

node.js - 如何在 CURL 上的表单字段中发送文件内容?

curl - 什么是 cURL -s 选项?

curl - 通过curl的PUT索引映射导致错误(Content-Type header 修复不起作用)