views:

81

answers:

4

Hi, How do I do this on a php webpage?

I want to get and decode a json string and display the results as html on my page, however, I don't want it hotlinking back to the source.

If I could write the decoded string to a txt file say weather.txt on the server and keep the html formatting and do it so that the page won't fetch the json script until 60 minutes has passed since the last time it was fetched regardless of how many times the page is opened during that 60 minute period and the weather.txt is viewed.

All I can come up with is a simple script that hotlinks, everything else I have tried simply failed.

$file = file_get_contents('http://sample.com/weather');
$out = (json_decode($file));
echo $out->mainText;

Will appreciate any help with this.

+1  A: 

You could use filemtime() to re-make weather.txt every minutes and if not, send the existing file.

Dan Heberden
+1  A: 

You can use an UNIX cronjob which overwrites the content of weather.txt every hour with what your code outputs

greg0ire
+1  A: 

You could also store the data in a 2 column table, 1 is data, one is dts.

SO....

SELECT * FROM table WHERE dts > NOW() - INTERVAL 60 MINUTE

if($mysqli->num_rows == 0)
{
   $file = file_get_contents('http://sample.com/weather');
   REPLACE INTO table SET data="$file",dts=NOW();
   $out = json_decode($file);
}
else
{
   $out = json_decode($mysqli->data);
}
echo $out->mainText; 

replace the SQL statements with your RDBMS of choice

Geek Num 88
A: 

Sorry folks for being so dumb but I will need the full script from start to finish for my php page as the more I read, the more confused I become.

Gary
I think that I have it sorted, using a timer set to 3600 seconds that won't check for an update until the time is up and someone opens the page after that, fopen txt file, fwrite to txt file, the page reads the script from the txt using json decode. Many thanks for everyones input.
Gary