【问题标题】:Curl_exec returns 404 not found error whereas url is found in browserCurl_exec 返回 404 not found 错误,而 url 在浏览器中找到
【发布时间】:2019-02-07 06:37:00
【问题描述】:

我正在尝试通过 curl 访问网址。我系统地收到特定 url 的 404 错误,而该页面是由我的浏览器找到的。 我尝试在调用 curl_exec 之前将用户代理和 cookie 设置为与浏览器进行的调用一样接近,但未能成功摆脱 404 错误(这些是在 stackoverflow 中发布的类似问题的解决方案,但似乎没有在我的情况下工作;参见cURL returns 404 while the page is found in browser)。

$url = "http://www.zenaps.com/rclick.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
$cookiefile = './cookie.txt'; // contains cookies saved after opening url in browser through Chrome addin cookies.txt
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
$response = curl_exec($ch);
echo $response;

您知道我可以做些什么来访问此网址而不会出现 404 错误吗?

谢谢!

【问题讨论】:

  • 您的 php 托管在哪里?也许IP被列入黑名单?
  • 它托管在本地主机上。而且我可以在同一台机器上毫无问题地在浏览器中访问此 url

标签: php curl http-status-code-404


【解决方案1】:

当您将 CURLOPT_NOBODY 选项设置为 true 时,这意味着 cURL 将执行 HEAD 请求而不是 GET 请求。

该 URL 似乎在 GET 请求上给出了 200,但在 HEAD 请求上给出了 404。

尝试将CURLOPT_NOBODY 设置为false。

【讨论】:

    【解决方案2】:

    试试这个:

    // create curl resource 
    $ch = curl_init(); 
    // set url 
    curl_setopt($ch, CURLOPT_URL, "http://www.zenaps.com/rclick.php"); 
    //return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // $output contains the output string 
    $output = curl_exec($ch); 
    echo($output);
    // close curl resource to free up system resources 
    curl_close($ch); 
    

    【讨论】:

      猜你喜欢
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 2018-12-16
      相关资源
      最近更新 更多