Question

i am using PHP to read feed xml in every half an hour, as of now i am always reading whole feed file whether it is updated or not. But i want to check whether feed xml is update since last download, if updated then only read the xml otherwise not.

I am trying to achieve it using below code, but $lastmodifiedRSS is always empty. I am not sure what is going wrong in my code. If i get $lastmodifiedRSS then i can easily compare it with last loaded time and then decide what to do.

If any Expert can share some info that would be great. Thank you in advance.

//get feed information & content
$feed = new feed($rssurl);
$feed->load();

//get the last modified date (web server information)
$lastmodifiedRSS = $feed->http->get_header_value('Last-Modified');

        function http($url)
{
    $this->url($url);
    $this->user_agent = 'posh';
    $this->header = "";
    $this->code = "";
    $this->codeStr = "";
    $this->max_d = 5;
    $this->authorization = "";
    $this->proxy_auth = "";
    $this->url = $url;
    $this->addProxy();
}

    function feed($url)
{
    if ($url) {
        $this->url = $url;
        $this->http = new http($url);
        //$this->addProxy();
    } else {
        $this->http = new http('');      
    }
}
function load()
    {
        $content= $this->http->get();
        if (!$content)
            return false;
        $content = $this->transcode($content);
        return $content;
    }

function get_header_value($name)
    {
        if (preg_match("/$name: ?([^\\r\\n]*)\\r\\n/m",$this->head,$matches) !== false)
        {
            if (count($matches) > 1)
            {
                return $matches[1];
            }
        }
        return false;
    }

Regards, Mona

Was it helpful?

Solution

Make use of stat() in PHP

<?php
print_r(stat('users.json'));

OUTPUT:

Array ( [0] => 2 [1] => 0 [2] => 33206 [3] => 1 [4] => 0 [5] => 0 [6] => 2 [7] => 298 [8] => 1384073940 [9] => 1384073940 [10] => 1368626190 [11] => -1 [12] => -1 [dev] => 2 [ino] => 0 [mode] => 33206 [nlink] => 1 [uid] => 0 [gid] => 0 [rdev] => 2 [size] => 298 [atime] => 1384073940 [mtime] => 1384073940 [ctime] => 1368626190 [blksize] => -1 [blocks] => -1 )

Source

Keeping a track of the variables [atime] , [size] can help you achieve what you are trying to do.

OTHER TIPS

This kind of checking are better handled with HTTP headers itself.

If the server sends Etag header in the response, you could use it with the If-None-Match header on the next request to know if the file has changed.

Similarly, if the server sends Last-Modified header in the response, you could use this timestamp with the If-Modified-Since header on the next request to know if the file got changed.

Just to let you know what you could do, I can't contribute any code cause I don't know which HTTP library you're using here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top