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
Solved

Get the last modified header from a web page

Posted on 2004-08-10
5
302 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
  • 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 200 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.

792 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