?
Solved

Get the last modified header from a web page

Posted on 2004-08-10
5
Medium Priority
?
338 Views
Last Modified: 2012-06-21
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?

0
Comment
Question by:stuayre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11761098
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
0
 
LVL 5

Accepted Solution

by:
TheClickMaster earned 800 total points
ID: 11767200
I presume the code you pasted here works and will reuse some code

function get_headers($host, $path = "/")
{
    $data  = "HEAD ".$path." HTTP/1.0\r\n";
    $data .= "HOST: ".$host."\r\n";
    $data .= "Connection: close\r\n";
    $data .= "\r\n";

    $fp = fsockopen ("$host", 80, &$errnr, &$errstr) or die("$errno: $errstr");
    fputs($fp,$data);

    $response = "";

    while (!feof($fp))  
    {
        $line = fgets($fp, 2048);
        $response .= $line;
    }

    fclose($fp);

    //display response
    echo $response."<br><br>";

    //try to find last-modified value
    $arr = explode("\r\n", $response);
    $size=count($arr);

    $result = "";

    for($n=0;$n<$size;$n++)
    {
        if(strtolower(substr($arr[$n],0,13)) == "last-modified") $result = substr($arr[$n],15);
    }

    return $result;  
}

returns an empty string or the result
0
 
LVL 1

Author Comment

by:stuayre
ID: 11769318
Thanks TheClickMaster, How would you call the function?
0
 
LVL 5

Expert Comment

by:TheClickMaster
ID: 11769903
<?php

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

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


?>
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11791068
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question