URL在浏览器和Postman中起作用,但在cURL中不起作用

问题描述 投票:0回答:1

我对这个想法一无所知。它是一个简单的curl请求,并且不起作用。网址是

https://www.nseindia.com/api/event-calendar?index=equities&from_date=05-05-2020&to_date=05-05-2020

在浏览器中工作正常。我将其放入Postman(简单的GET请求)中,并且在那里也可以工作。我从邮递员本身生成PHP代码,但它不起作用。该代码一直运行直到超时,然后出现500个内部服务器错误。在Apache日志中,我得到此

[Mon May 04 18:47:59.325513 2020] [fcgid:warn] [pid 25143] (104)Connection reset by peer: [client 112.196.159.208:47585] mod_fcgid: error reading data from FastCGI server
[Mon May 04 18:47:59.325568 2020] [core:error] [pid 25143] [client 112.196.159.208:47585] End of script output before headers: test.php

我还创建了此playground以确保问题不是我的服务器而是相同的结果。

为什么它不能在cURL中使用?

php php-curl
1个回答
0
投票

正如@Evert所提到的,它们阻止了非浏览器的请求,因此您可以通过在User-Agent后面附加键:CURLOPT_USERAGENT来欺骗您的请求>

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_USERAGENT   => "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0",
  CURLOPT_URL => "https://www.nseindia.com/api/event-calendar?index=equities&from_date=05-05-2020&to_date=05-05-2020",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
));

$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);

if($error) {
    echo "cURL Error:" . $error;
} else {
    echo $response;
}
© www.soinside.com 2019 - 2024. All rights reserved.