1

This is not duplicate because I made curl follow redirects and the output is 302 http status code

I have this code

<?php 

$url="http://www.facebook.com/thisfburldoesnotexist";
$handle = curl_init($url);
curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
echo $httpCode;
curl_close($handle);
?>

to grab the status code of some pages, but when I try to get the status of this http://www.facebook.com/thisfburldoesnotexist the response is 302, but Chrome report http 404 when debugging enter image description here

how can I get the actual 404 reported in Chrome?

If I check the url with this tool http://www.websitepulse.com/help/tools.php it reports what I want: 404 - Not Found

Gigi Ionel
  • 248
  • 1
  • 10

1 Answers1

1

What happens is, when you request that non-existent page, facebook responds with a page with status code 302 or 307. Then, it redirects to another page with status code 404.

So you need to add this curl option so curl will follow that redirect, and eventually land on the 404 page.

curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);

By default, this parameter is set to 0, so, just like in your case, it does not follow the redirect and the response you get is just the 302 or 307 or whatever facebook decides to send as a first response. By setting it to 1 it will follow the redirect.

more info here: http://curl.haxx.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html

just noticed, you can actually see what im saying in your screenshot. the first response you are getting is a 307.

Chrome browser and that tool on that website do honor the redirect request, curl does not unless configured as described above.

Sharky
  • 6,154
  • 3
  • 39
  • 72
  • If I ask you to get the 404 code with php, how would you do that in PHP? I've tried in so many ways but didn`t worked for me... – Gigi Ionel Sep 10 '15 at 13:40