Solved

Get the last modified header from a web page

Posted on 2004-08-10
5
277 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:Squinky
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:Squinky
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now