简体   繁体   中英

Curl_exec returns 404 not found error whereas url is found in browser

I am trying to reach an url through curl. I systematically get a 404 error for a specific url whereas the page is found by my browser. I tried to set user agent and cookies before calling curl_exec to be as close as the call made by a browser, but did not succeed to get rid of 404 error (these were the solutions to similar questions posted in stackoverflow, but it does not seem to work in my case; 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;

Do you have any idea what I could do to reach this url without having 404 error?

Thanks!

Try this:

// 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); 

When you set the CURLOPT_NOBODY option to true , it means cURL will do a HEAD request instead of a GET request.

The URL seems to give a 200 on GET requests, but a 404 on HEAD requests.

Try setting CURLOPT_NOBODY to false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM