2

I am working on getting the header response codes for a list of urls. I need the output to be like HTTP/1.1 200 OK etc.

Using cURL - I used this command curl -s -I https://www.pnc.com | grep HTTP (or) curl -I https://www.pnc.com 2>/dev/null | head -n 1. And, I get the output as HTTP/1.1 200 OK.

Using PHP - I used get_headers() command to do the same in PHP. And I'm getting the output as HTTP/1.1 301 Moved Permanently.

This is the code that I used.

$line = "https://www.pnc.com";
$myheader = get_headers($line,1);
echo "$myheader[0]";

After running this, I get the output as HTTP/1.1 301 Moved Permanently. Can someone give any suggestions?

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
Joe14_1990
  • 89
  • 1
  • 9
  • 1
    Because that's what the site responds with? `HTTP/1.1 301 Moved Permanently` and `Location: https://www.pnc.com/en/` – Phil Sep 19 '18 at 23:04
  • 2
    See the note here ~ http://php.net/manual/function.get-headers.php#100113 – Phil Sep 19 '18 at 23:05

1 Answers1

0

Your command line invocation using curl is following the 301 redirect and then returning the headers of the redirected page. For a more reliable approach using curl from PHP I suggest you use a variation of my answer over here:

Can PHP cURL retrieve response headers AND body in a single request?

If you wish to transparently follow the redirect as curl is doing on the command line, set the curl option CURLOPT_FOLLOWLOCATION, for example:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Since you are not interested in the body, turn off the return transfer option, ie:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

If you wish to perform a HEAD request set the following option also:

curl_setopt($ch, CURLOPT_NOBODY, true);
Geoffrey
  • 10,843
  • 3
  • 33
  • 46
  • Thank you so much for the suggestion. When I added the HEAD request set it gives me the correct output. Thank you again. – Joe14_1990 Sep 20 '18 at 15:16