phpcurlhttp-status-code-404

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!


Solution

  • 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.