How can I force PHP's fopen() to return the current version of a web page?
- by Edward Tanguay
The current content of this google docs page is:
However, when reading this page with the following PHP fopen() script, I get an older, cached version:
I've tried two solutions proposed in this question (a random attribute and using POST) and I also tried clearstatcache() but I always get the cached version of the web page.
What do I have to change in the following script so that fopen() returns the current version of the web page?
<?php
$url = 'http://docs.google.com/View?id=dc7gj86r_32g68627ff&rand=' . getRandomDigits(10);
echo $url . '<hr/>';
echo loadFile($url);
function loadFile($sFilename) {
clearstatcache();
if (floatval(phpversion()) >= 4.3) {
$sData = file_get_contents($sFilename);
} else {
if (!file_exists($sFilename)) return -3;
$opts = array('http' =>
array(
'method' => 'POST',
'content'=>''
)
);
$context = stream_context_create($opts);
$rHandle = fopen($sFilename, 'r');
if (!$rHandle) return -2;
$sData = '';
while(!feof($rHandle))
$sData .= fread($rHandle, filesize($sFilename));
fclose($rHandle);
}
return $sData;
}
function getRandomDigits($numberOfDigits) {
$r = "";
for($i=1; $i<=$numberOfDigits; $i++) {
$nr=rand(0,9);
$r .= $nr;
}
return $r;
}
?>