Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get the last modified header from a web page

Posted on 2004-08-10
5
Medium Priority
?
341 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

610 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