【问题标题】:Failed to call the browser based request using PHP Curl使用 PHP Curl 调用基于浏览器的请求失败
【发布时间】:2020-05-07 09:47:33
【问题描述】:

如果我们在浏览器中调用相同的 URL,它会自动下载 CSV 文件。所以,我想要使用 PHP curl 的相同功能将 CSV 文件保存在同一文件夹下。但它每次都给我一个空的结果。你能指导我下面的代码有什么问题吗?

$url="https://www.centrano.com/catalog_download.php?email=info@sporttema.com&password=dHB3L1FpTEg1c2pLZ29SUkdnUWcwWTFqN2RIamQx";

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
$agent= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100000); //time out of 15 seconds
$output = curl_exec($ch); 
print_r($output);
curl_close($ch);

【问题讨论】:

    标签: php curl file-get-contents php-curl


    【解决方案1】:

    添加以下两个 CURL 选项以使其工作:

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);                                 
    curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');   
    

    页面多次重定向到同一主机,并要求会话 cookie 保持存在(因此,将它们存储在 cookie jar 中。

    另外:立即更改您的 centrano.com 帐户密码!尽管这有助于解决这个问题,但通常将其公开并不是一个好主意。

    【讨论】:

      猜你喜欢
      • 2013-03-19
      • 1970-01-01
      • 2016-08-02
      • 2023-03-10
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      相关资源
      最近更新 更多