javascript - How to automatically call an rest-api in wordpress - Stack Overflow

I'm using wp-get api plugin to call an open rest-api , data returned by that api is changing conti

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 .

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 .

Share Improve this question asked May 8, 2023 at 18:12 Prince BhatiPrince Bhati 1051 gold badge5 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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./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./a/5687625/2663707

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745144198a4613572.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信