2

Hej there,

I am currently working on a PHP-based Tool to monitor a rather large number of URLs and their redirect status. I have spent quite some time on finding the best way to fetch the content of the HTTP response headers to extract the current redirect code and location. This is how it's done at the moment:

$resource = fopen( $url, 'r' );
$metadata = stream_get_meta_data( $resource );
$metadata = $metadata['wrapper_data'];

// Looping through the array to find the necessary fields

This works on 95% of the URLs I'm monitoring. For a few more I have solved it by parsing the actual HTML the website returns before the redirect is executed since it contained something like "This website has been moved here". This does not seem to be a very robust solution, but it helped in a few cases.

This still leaves me with a number of URLs I can not check automatically.

Tools like Ask Apache HTTP Headers Tool seem to be more reliable and I was wondering what could be a better way to obtain the redirect information?

2
  • stackoverflow.com/questions/270918/… Commented Aug 31, 2009 at 9:44
  • Sorry, that one did not show up in the list of similar questions. However, there's a new solution to the question on here now. Commented Aug 31, 2009 at 10:22

1 Answer 1

6

You could also try out curl, shortest possible example that retrieves all the headers looks like this:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://stackoverflow.com');
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);

function read_header($ch, $string) {
    print "Received header: $string";
    return strlen($string);
}

Output:

[~]> php headers.php 
Received header: HTTP/1.1 200 OK
Received header: Cache-Control: private
Received header: Content-Type: text/html; charset=utf-8
Received header: Expires: Mon, 31 Aug 2009 09:38:45 GMT
Received header: Server: Microsoft-IIS/7.0
Received header: Date: Mon, 31 Aug 2009 09:38:45 GMT
Received header: Content-Length: 118666
Received header: 

Of course, it is just headers you want, then fsockopen works just as well. Except that instead of GET, you should use HEAD, because you just want the headers, not the content.

Also, curl works (provided you have compiled it with ssl support) for https url-s as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.