Jump to content

receiving cached XML data with file_get_contents()


liquidskin

Recommended Posts

Hi all,

 

I should first mention I'm not much of a coder, I'm using PHP to create a custom weather solution for myself.  Basically am pulling XML from weather.gov and working with the data.

 

So far, so good.  I'm getting the data I want displayed correctly, but noticed when I refresh the page I sometimes receive old data. (From the past hour, two hours, etc.)  I figured this was cached info and I'm trying to figure out how to clear that out.

 

This is how I'm accessing the XML:

$url = 'http://forecast.weather.gov/MapClick.php?lat=40.65160&lon=-74.34420&FcstType=digitalDWML';
$xml = file_get_contents($url);

 

I did some research and tried the following headers, but that doesnt seem to work:

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>

 

Also tried appending a random number onto the $url, (as per a forum question/response somewhere) but that didn't work.

 

Anyone suggestions would be great.

 

Thanks

 

 

 

Did you append just a random number to $url, or did you append something similar to "&random=" . random number? I don't think the info would be cached at all...

 

i tried once more and it seems to work this way:

$url = 'http://forecast.weather.gov/MapClick.php?lat=40.65160&lon=-74.34420&FcstType=digitalDWML&'.rand(1,1000);

 

thanks!

 

hi all,

 

I decided to dump the rand() method on the URL for file_get_contents and to try cURL.

 

However, I'm having the same issue.

 

<?php

class CA_HTTP {

    public static function get_contents($url) {
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
        ob_start();
        curl_exec ($ch);
        curl_close ($ch);
        return ob_get_clean();  
    }

}
?>

 

 

$xml = CA_HTTP::get_contents($url);

 

I was under the impression that CURLOPT_FRESH_CONNECT would do the trick.  Anyone know why I'm receiving old data?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.