最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览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 .

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.

发布评论

评论列表(0)

  1. 暂无评论