Solved

Get the last modified header from a web page

Posted on 2004-08-10
5
297 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to count in a table in php 22 36
mysql date time 14 31
PHP insert data in to database... this is not letting me in insert data...please help! 7 42
Cookie not unsetting 7 20
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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

810 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