Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. // lets define the working files
  2. define("FILE_API_TIMESTAMP", "api.timestamp.txt");
  3. define("FILE_API_DATA", "api.data.json");
  4. // initialize main vars
  5. // last api call
  6. $last_call = 0;
  7. // latest api data
  8. $api_data = false;
  9. // this moment timestamp
  10. $current_time = time();
  11. // check last api call from saved file
  12. if (file_exists(FILE_API_TIMESTAMP)) {
  13. $last_call = floatval( file_get_contents( FILE_API_TIMESTAMP ) );
  14. }
  15. // if more than 60 seconds passed since last call,
  16. // call API then save results
  17. if ($current_time - $last_call > 60) {
  18. // get new data
  19. // $api_data = getApiData(); -> put your function here: Curl etc...
  20. $api_data = "new data: ".time(); // just an example
  21. if ($api_data) {
  22. // if new data is available,
  23. // - log the current timestamp
  24. file_put_contents(FILE_API_TIMESTAMP, $current_time);
  25. // update $last_call for later use
  26. $last_call = $current_time;
  27. // - save the new data
  28. file_put_contents(FILE_API_DATA, $api_data);
  29. }
  30. }
  31. // check if we have new data, if not bring old data
  32. if (!$api_data) {
  33. if (file_exists(FILE_API_DATA))
  34. $api_data = file_get_contents(FILE_API_DATA);
  35. else
  36. $api_data = "no_data";
  37. }
  38. // finally, give the user the updated data:
  39. echo "Data updated at: ".date("d-m-Y H:i:s", $last_call);
  40. echo "<hr/>";
  41. echo $api_data;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement