Curl_exec returns 404 未找到错误而 url 在浏览器中找到
Curl_exec returns 404 not found error whereas url is found in browser
我正在尝试通过 curl 达到 url。我系统地收到特定 url 的 404 错误,而该页面是由我的浏览器找到的。
我尝试在调用 curl_exec 之前将用户代理和 cookie 设置为与浏览器进行的调用一样接近,但没有成功摆脱 404 错误(这些是 Whosebug 中发布的类似问题的解决方案,但是它在我的情况下似乎不起作用;cf. 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;
你知道我可以做些什么来达到这个 url 而不会出现 404 错误吗?
谢谢!
试试这个:
// 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);
当您将 CURLOPT_NOBODY
选项设置为 true
时,这意味着 cURL 将执行 HEAD 请求而不是 GET 请求。
URL 似乎在 GET 请求上给出了 200,但在 HEAD 请求上给出了 404。
尝试将 CURLOPT_NOBODY
设置为 false
。
我正在尝试通过 curl 达到 url。我系统地收到特定 url 的 404 错误,而该页面是由我的浏览器找到的。 我尝试在调用 curl_exec 之前将用户代理和 cookie 设置为与浏览器进行的调用一样接近,但没有成功摆脱 404 错误(这些是 Whosebug 中发布的类似问题的解决方案,但是它在我的情况下似乎不起作用;cf. 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;
你知道我可以做些什么来达到这个 url 而不会出现 404 错误吗?
谢谢!
试试这个:
// 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);
当您将 CURLOPT_NOBODY
选项设置为 true
时,这意味着 cURL 将执行 HEAD 请求而不是 GET 请求。
URL 似乎在 GET 请求上给出了 200,但在 HEAD 请求上给出了 404。
尝试将 CURLOPT_NOBODY
设置为 false
。