简体   繁体   English

如何使用php ajax进行file_get_contents和刷新数据而无需重新加载页面

[英]How to file_get_contents & refresh data without page reload using php ajax

I am taking some json data from an API using php. 我正在使用php从API中获取一些json数据。 I'd like the data to refresh every 20 seconds without having to manually reload the page. 我希望数据每20秒刷新一次,而无需手动重新加载页面。 This is my code below that works great at fetching the data. 这是我下面的代码,非常适合于获取数据。 Just need to figure out how to refresh - maybe with ajax? 只需要弄清楚如何刷新-也许使用Ajax?

$getdata = file_get_contents("https://data.website.com/");
$datajson = json_decode($getdata);
$mydata = $datajson->whatiwant;
echo $mydata;

request your data using AJAX: 使用AJAX请求您的数据:

function requestMyData() {
    $.ajax({
        url: "/url/to/interface.php",
        type: get,
        success: function(data) {
            //update your UI here
        }
    });
}

you are then able to request the data using a timeout: 然后,您可以使用超时请求数据:

setTimeout(requestMyData(), 100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM