0

I'm brand new to php and curl. I have a php function which is retrieving data from an API utilizing curl. This is working correctly. However, the APIs data will be updating, so I will need to periodically retrieve the json-formatted data.

function call_external_api()
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, sprintf('https://my-api.com'));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $ex = curl_exec($curl);
    $data = json_decode($ex, true);
    $info = curl_getinfo($curl);
    return $data;
}

So basically if I want to call this php function every 10 seconds, how would I go about doing so?

Austin Burke
  • 148
  • 2
  • 10
  • What happens to the returned `$data`? Is it displayed on an HTML page? You might consider using [AJAX](https://stackoverflow.com/questions/1510011/how-does-ajax-work) or [cron](https://stackoverflow.com/questions/22358382/execute-php-script-in-cron-job). – showdev Jul 16 '19 at 01:50
  • @showdev Eventually, yes. The $data is a nested json object which has names and various values. Eventually it is looped through and the relevant data is displayed in HTML. – Austin Burke Jul 16 '19 at 01:52
  • In that case, I'd suggest using [asynchronous JavaScript](https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX) that fires [on a timer](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout) to call your PHP file. The data can be returned to your JavaScript script, which can then display the information as needed. – showdev Jul 16 '19 at 01:54
  • @Austin Burke then update your data in table and be done with that, or check (i assuming you do this through javascript) check the first data in table and last data in $data var, (all depends on order they come) and update your HTML if its different. – Serghei Leonenco Jul 16 '19 at 01:54

1 Answers1

0

Since you'll be outputting the returned data on an HTML page, I suggest using JavaScript to asynchronously call your PHP file. You can fire your JavaScript call on a timer.

Depending on the API, you might not even need PHP as an intermediary.

Here's a basic example:

function getRandomNumber(min, max) {
  // Generate a random number between min and max.
  // Only for demonstration purposes.
  return Math.floor(Math.random() * (max - min) + min);
}

function makeRequest() {
  // Fetch test data by random ID.
  // This is where you call your PHP file.
  let id = getRandomNumber(1, 10);
  oReq.open("GET", "https://reqres.in/api/users/" + id);
  oReq.send();
}

function handleResponse() {
  // Output returned data to page
  output.innerHTML = JSON.parse(this.responseText).data.email;
  // Set up the next request to fire again in two seconds
  setTimeout(makeRequest, 2000);
}

// Define output element
let outout = document.getElementById('output');

// Define XMLHttp Request
let oReq = new XMLHttpRequest();
oReq.addEventListener("load", handleResponse);

// Make first request
makeRequest();
<div id="output"></div>

For reference, see:

showdev
  • 28,454
  • 37
  • 55
  • 73