【问题标题】:get request works on postman but not with file_gets_content and cURL from php获取请求适用于邮递员,但不适用于来自 php 的 file_gets_content 和 cURL
【发布时间】:2016-10-29 19:28:53
【问题描述】:

这是下面的 url,用 file_get_contents 和 curl 方法都试过了……在 php 脚本中不起作用,但在 postman 中可以正常工作。 知道为什么会这样吗???

https://query.yahooapis.com/v1/public/yql?q= select * from weather.forecast where woeid in (SELECT woeid FROM geo.places WHERE text="(28.56,77.32)") and u='c'&format=json

cURL请求如下:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

$output = curl_exec($ch);   

// convert response
$output = json_decode($output);

// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {

  var_dump($output);
}
echo curl_error($ch);

curl_close($ch);

【问题讨论】:

  • file_get_contents 不适用于 https
  • 请为请求添加您的 curl 代码。
  • edited..with curl

标签: php curl postman


【解决方案1】:

试试这个。不同的是url是url编码的:

$url = 'https://query.yahooapis.com/v1/public/yql?q=%20select%20*%20from%20weather.forecast%20where%20woeid%20in%20(SELECT%20woeid%20FROM%20geo.places%20WHERE%20text=%22(28.56,77.32)%22)%20and%20u=%27c%27&format=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

$output = curl_exec($ch);

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 2021-05-24
    • 2019-07-03
    • 2021-10-06
    • 1970-01-01
    • 2021-08-04
    • 2020-11-20
    • 1970-01-01
    • 2020-06-13
    相关资源
    最近更新 更多