0

I want to find the final redirected status codes for a webpages in a certain site. After given expiry date this site deletes these pages and redirects you to its home page with 404 status code.

Let's say one of the pages I want to go is http://example.com/my-page and this page does not exist anymore. So it will redirect me to http://example.com/. I checked with chrome developer tools and it sends a 404 status code.

Now here is the problem. I want to check whether this page is a 404 or not. I have following PHP function.

    function findStatus($url)
    {
        $handle = curl_init($url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);

        /* Check for 404 (file not found). */
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

        curl_close($handle);

        if ($httpCode == 404) {
            return true;
        } else {
            return false;
        }

    }

When I check the relevant page with above function I get 302 for permanent redirection. I guess this function gets the status code before redirection.

This answer describes how to get the URL, but not status code. How do I get status code after page's redirection complete?

Community
  • 1
  • 1
Janaka Dombawela
  • 1,337
  • 4
  • 26
  • 49

1 Answers1

0

Since you find that answer: Get final redirect with Curl PHP

You are very close to it. curl_getinfo() can get a lot of information.

$url = 'http://localhost:8080/loc.php';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$html = curl_exec($ch);

$info = curl_getinfo($ch);

curl_close($ch);

print_r($info);
# Array
# (
#     [url] => http://localhost:8080/dest.php
#     [content_type] => text/html; charset=UTF-8
#     [http_code] => 404
Community
  • 1
  • 1
XiaoChi
  • 369
  • 4
  • 4