Link to home
Start Free TrialLog in
Avatar of stuayre
stuayre

asked on

Get the last modified header from a web page

How can I read the last modified header of a web page on the net?

I don't want to fetch the whole content just the header

I tried

function get_headers($host, $path = "/")
{
$fp = fsockopen ("$host", 80, &$errnr, &$errstr) or die("$errno: $errstr");
fputs($fp,"GET $path HTTP/1.0\n\n");
while (!$end)
{
$line = fgets($fp, 2048);
if (trim($line) == "")
$end = true;
else
echo $line;
}
fclose($fp);
}


but I just get 'Permission denied' errors or 'Invalid URI in request GET HTTP/1.0'

am i doing it right? is there a better way?

Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Use CURL:

http://www.php.net/manual/en/ref.curl.php

There is an example of exactly how to do this in the user notes on that page by thomas at NOSPAM dot gutschke dot com
ASKER CERTIFIED SOLUTION
Avatar of TheClickMaster
TheClickMaster
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stuayre
stuayre

ASKER

Thanks TheClickMaster, How would you call the function?
<?php

$result = get_headers("www.google.com","/");

echo "Google.Com Last modified: ".($result != "" ? $result : "Unknown");


?>
Bear in mind that this code will not work with any URLs that cause a redirect. That's why CURL has the CURLOPT_FOLLOWLOCATION option, and it's hard work to emulate using sockets.