liquidskin Posted October 9, 2010 Share Posted October 9, 2010 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 Link to comment https://forums.phpfreaks.com/topic/215504-receiving-cached-xml-data-with-file_get_contents/ Share on other sites More sharing options...
phpeter Posted October 9, 2010 Share Posted October 9, 2010 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... Link to comment https://forums.phpfreaks.com/topic/215504-receiving-cached-xml-data-with-file_get_contents/#findComment-1120622 Share on other sites More sharing options...
liquidskin Posted October 10, 2010 Author Share Posted October 10, 2010 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! Link to comment https://forums.phpfreaks.com/topic/215504-receiving-cached-xml-data-with-file_get_contents/#findComment-1120693 Share on other sites More sharing options...
liquidskin Posted October 15, 2010 Author Share Posted October 15, 2010 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? Link to comment https://forums.phpfreaks.com/topic/215504-receiving-cached-xml-data-with-file_get_contents/#findComment-1122489 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.