0

I'm using wp-get api plugin to call an open rest-api , data returned by that api is changing continously , i also want to show that live data , without calling api or reloading the page .

<div class="box"  id="heart">
  <div class="glass"></div>
    <div class="content">
      <h1><?php
              $data = wpgetapi_endpoint( 'yahoo', 'yahoo', array('debug' => false) );
              $result = intval($data["tabsOpened"]/15);
              echo $result;
            ?> 
        </h1> 
        <p>Food Plates Donated</p>  
     <h1><?php
              $data = wpgetapi_endpoint( 'yahoo', 'yahoo', array('debug' => false) );
              $result = intval($data["tabsOpened"]);
              echo $result;
            ?> 
        </h1> 
        <p>Tabs Opened</p>  
  </div>
</div>

i'm showing the data in heart, i want to update the values like a counter . how can i achieve this .

enter image description here

Prince Bhati
  • 73
  • 1
  • 8

2 Answers2

1

To automatically call a REST API in WordPress, you can use the wp_remote_get function. This function allows you to send an HTTP GET request to a remote API endpoint and retrieve the response.

Here is an example code snippet that demonstrates how to use wp_remote_get to call a REST API:

$url = 'https://example.com/api/endpoint';
$response = wp_remote_get( $url );
    
if ( is_wp_error( $response ) ) {
   // handle Error 
} else {
  $body = wp_remote_retrieve_body( $response );
  // do something with the API response
}

Here you can find an explation how you call an API every x seconds/minutes with jquery

setInterval(function()
{ 
    $.ajax({
      type:"post",
      url:"yourapicall",

      success:function(data)
      {
          //do something with response data
      }
    });
}, 10000);//time in milliseconds 

https://stackoverflow.com/a/5687625/2663707

Auraya
  • 51
  • 4
  • yes but continually calling the api in this way is not good practice. The good solution is to use Webhook. So, when data change, it automatically trigger and send a request to application. – Developer May 08 '23 at 19:01
1

As the data of the api is changing, so definitely you have to setup some Webbook. Otherwise you have to call the api again and again which is definitely not a good practice. The solution is Webhook.

Also to update the data without reloading the page, you have to setup AJAX also.

With AJAX and Webhook you will acheive the desired results.

Developer
  • 1,297
  • 1
  • 4
  • 15
  • how can i setup webhook. – Prince Bhati May 08 '23 at 19:25
  • which api are you using?? Can you tell the name? – Developer May 08 '23 at 19:30
  • i'm using this api https://tab.pledgeasmile.com/backend/api/tabsInfo/global – Prince Bhati May 08 '23 at 19:32
  • @PrinceBhati I don't know which api is this. Normally, the api's provide the webhook setup. So, you just need to tell the application url and the api will trigger that. So, it is upto the api. You can only create a endpoint in wordpress which will receive the data. So, you have to look at the api documentation. But if they have no such setup, then simply use ajax and setInterval in your javascript to get the data and update your page. – Developer May 08 '23 at 19:38