Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-type: text/xml');
- $threshold = 600; // 10 minutes in seconds
- $myApiUrl = 'http://myapisite.com/rss'; //rss feed
- $file = 'filename.txt';
- $fmtime = filemtime($file);
- $timeDiff = time() - $fmtime;
- /* gets the rss data from my api */
- function get_data_from_api($url) {
- $ch = curl_init();
- $timeout = 20;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- // If file exists (fmtime is true) and timediff is lower or equal to threshold, send file.
- if ($fmtime && $timeDiff <= $threshold) {
- $apiData = file_get_contents($file);
- echo $apiData;
- // else if file doesn't exist or timediff is over threshold, fetch api and save to file.
- } elseif (!$fmtime || $timeDiff > $threshold) {
- $apiData = get_data_from_api($myApiUrl);
- if($apiData !== false){
- file_put_contents($file, $apiData);
- echo $apiData;
- }
- } else {
- // Do whaetever if nothing else happens
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement