gpt4 book ai didi

Javascript setInterval 方法始终返回与 PHP 方法相同的结果,但我需要更改数据

转载 作者:行者123 更新时间:2023-12-03 05:32:40 25 4
gpt4 key购买 nike

所以我的问题是我需要更新来自其他站点的一些数据,并且为了调用该数据,我有 php 函数,其中 URL 作为参数。 ..所以在JS中我创建了一个与setInterval循环的函数,我用URL参数调用该php函数,数据存储在哪里,但它总是返回相同的数据..(数据实际上在流上播放轨道,所以数据改变了每 +- 3 分钟)数据仅在刷新页面 (f5) 上更改..但我需要在后台更新该数据..

这是 PHP 函数

function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
$data = str_replace(",,","},",$data);
$data = str_replace("}}]}}","}]}}",$data);
$data = str_replace("]}}","}]}}",$data);
$data = str_replace(",}}","}}}",$data);
$data = str_replace("}}]}}","}]}}",$data);
return $data;

在js中,我仅在setInterval循环中调用console.log来显示php函数的结果..

console.log(<?php echo (get_content("http://server1.internetoveradio.sk:8809/status-json.xsl"));?>["icestats"]["source"])

最佳答案

嗯,是的。在这种情况下,PHP 仅被调用一次,即您回显 get_content() 内容的那一次;

如果你想反复获取内容,可以使用XmlHTTPRequest调用PHP文件,然后返回get_content()的结果;

jQuery 实现了 ajax ( XmlHTTPRequest ) 来做到这一点。

jQuery.ajax({
url: "http://path.to/your_script.php",
method: "get",
complete: function( response ){
console.log(response);
}
});

编辑:创建一个新的 .php 文件并粘贴以下内容:

<?php

function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
$data = str_replace(",,","},",$data);
$data = str_replace("}}]}}","}]}}",$data);
$data = str_replace("]}}","}]}}",$data);
$data = str_replace(",}}","}}}",$data);
$data = str_replace("}}]}}","}]}}",$data);
return $data;
}

echo get_content("http://server1.internetoveradio.sk:8809/status-json.xsl");

在您的 html 中添加以下内容:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function(){
jQuery.ajax({
url: "http://path.to/your_script.php",
method: "get",
complete: function( response ){
console.log(response);
}
});
});
</script>

这是最基本的版本,但它应该能为您指明正确的方向。

关于Javascript setInterval 方法始终返回与 PHP 方法相同的结果,但我需要更改数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40876737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com