Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中没有来自长轮询的数据_Php_Jquery_Ajax_Long Polling - Fatal编程技术网

在PHP中没有来自长轮询的数据

在PHP中没有来自长轮询的数据,php,jquery,ajax,long-polling,Php,Jquery,Ajax,Long Polling,基本上,我正试图在我的网站上建立一个实时提要,以获得一个外部API,将所有新数据推送到我的数据库中 下面是我用来抓取PHP文件并将任何新内容添加到数据库中的当前HTML代码 $min_id_result = $DB->select("SELECT `id` FROM `api_media` ORDER BY `id` DESC",true); //Basically all this is doing is returning the latest ID in the database

基本上,我正试图在我的网站上建立一个实时提要,以获得一个外部API,将所有新数据推送到我的数据库中

下面是我用来抓取PHP文件并将任何新内容添加到数据库中的当前HTML代码

$min_id_result = $DB->select("SELECT `id` FROM `api_media` ORDER BY `id` DESC",true);
//Basically all this is doing is returning the latest ID in the database

var Id = "<?=$last_id_result['id'];?>";
function waitForPics() {
    $.ajax({
        type: "GET",
        url: "/ajax.php?id="+Id,

        async: true,
        cache: false,

        success: function(data){
            var json = eval('('+ data +')');
            var img = json['img'];
            if(json['img'] != "") {
                $('<li/>').html('<img src="'+img+'" />').appendTo('#container')
            }
            Id = json['id'];
            setTimeout(waitForPics,1000);
        },
     });
}

$(document).ready(function() {
    waitForPics();
 });
$min_id_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC",true);
$next_min_id = $min_id_result['id'];

$last_id = $_GET['id'];

while($next_min_id <= $last_id) {
    usleep(10000);
    clearstatcache();
}

$qry_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC");

$response = array();
foreach($qry_result as $image) {
    $response['img'] = $image['image'];
    $response['id'] = $image['id'];
}
echo json_encode($response);
下面是我用来处理数据库的ajax.php文件

$min_id_result = $DB->select("SELECT `id` FROM `api_media` ORDER BY `id` DESC",true);
//Basically all this is doing is returning the latest ID in the database

var Id = "<?=$last_id_result['id'];?>";
function waitForPics() {
    $.ajax({
        type: "GET",
        url: "/ajax.php?id="+Id,

        async: true,
        cache: false,

        success: function(data){
            var json = eval('('+ data +')');
            var img = json['img'];
            if(json['img'] != "") {
                $('<li/>').html('<img src="'+img+'" />').appendTo('#container')
            }
            Id = json['id'];
            setTimeout(waitForPics,1000);
        },
     });
}

$(document).ready(function() {
    waitForPics();
 });
$min_id_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC",true);
$next_min_id = $min_id_result['id'];

$last_id = $_GET['id'];

while($next_min_id <= $last_id) {
    usleep(10000);
    clearstatcache();
}

$qry_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC");

$response = array();
foreach($qry_result as $image) {
    $response['img'] = $image['image'];
    $response['id'] = $image['id'];
}
echo json_encode($response);
我遇到的问题是,它没有从请求中返回任何数据,对我来说似乎都是正确的,但显然,开发人员的第二眼有时会更好,这应该是可行的:

while($next_min_id <= $last_id) {
    usleep(10000);
    clearstatcache();
    $min_id_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC",true);
    $next_min_id = $min_id_result['id'];
}
当您等待某个特定参数更改时,您需要在内部为其提供更改的机会

变量本身不会改变,
您需要刷新它们。

您永远不会更新while循环中的任何内容,因此它永远不会给您提供新数据。你经常使用usleep的while循环来检查信息,在你的循环中,你所做的就是告诉统计缓存清除。你看过网络流量firebug、google chrome工具、ngrep、tcpdump。。。你能得到任何回应吗?@patrick,这就是我想要它做的,检查两个ID是否相同,然后清除stat cache以了解CPU原因等等。。如果不是,它将跳出while循环并执行文件的其余部分。一旦进入循环,它将在脚本/请求之前不会中断timeout@Curtis,那么您只需要一个if语句,它总是反复检查相同的值,如果$next_min_id小于或等于$last_id,它将永远循环,然后像DanFromGermany提到的那样超时。