简体   繁体   English

AJAX每5秒调用一次PHP脚本

[英]AJAX Call Every 5 seconds to PHP Script

I'm tapping into the Twitter firehose to store some tweets based on some set filters for analytic purposes. 我正在使用Twitter的firehose,以基于一些设置的过滤器存储一些推文,以进行分析。

I have written a PHP script that opens a connection to the Twitter stream, and keeps it open while(!eof)...so this basically stays open indefinitely. 我编写了一个PHP脚本,该脚本打开了与Twitter流的连接,并在while(!eof)保持打开状态。因此,这基本上是无限期保持打开状态。

However, I have it set so that it stops after 5 seconds. 但是,我对其进行了设置,使其在5秒钟后停止。 I want to use AJAX to call this script ever 5.5 seconds (offset to make sure they don't collide), and then re-loop on success etc... 我想使用AJAX每5.5秒调用一次此脚本(偏移量以确保它们不会发生冲突),然后在成功执行时重新循环等。

The problem is that my function doesn't seem to be receiving a "success" signal. 问题是我的函数似乎没有收到“成功”信号。 What's going on here? 这里发生了什么?

Here are the relevant portions of my code: 这是我的代码的相关部分:

$(function() {
    makeRequest();
});

function makeRequest(){
    console.log("Getting tweets...");
    $.ajax({
        url: "./php/store_tweets.php",
        success: function(){
            console.log("Success!");
            makeRequest();
        }
    });
}

The Script: 剧本:

<?php
$start      = time();
$expAddress = "HOSTNAME";
$expUser    = "USERNAME";
$expPwd     = "PASSWORD";
$database   = "DBNAME";

$opts = array(
    'http' => array(
        'method' => "POST",
        'content' => 'keywords,go,here'
    )
);

// Open connection to stream
$db = mysql_connect($expAddress, $expUser, $expPwd);
mysql_select_db($database, $db);

$context  = stream_context_create($opts);
$instream = fopen('https://USERNAME:PASSWORD@stream.twitter.com/1/statuses/filter.json', 'r', false, $context);
while (!feof($instream)) {

    if (time() - $start > 5) { // break after 5 seconds
        break;
    }


    if (!($line = stream_get_line($instream, 100000, "\n"))) {
        continue;
    } else {
        $tweet = json_decode($line);

        // Clean before storing             

        // LOTS OF VARIABLES FOR BELOW...REMOVED FOR READABILITY

        // Send to database
        $ok = mysql_query("INSERT INTO tweets 
                    (created_at, from_user, from_user_id, latitude, longitude, tweet_id, language_code, 
                            place_name, profile_img_url, source, text, retweet_count, followers_count,
                            friends_count, listed_count, favorites_count) 
                    VALUES 
                    (NOW(), '$from_user', '$from_user_id', '$latitude', '$longitude', '$tweet_id', '$language_code', 
                            '$place_name', '$profile_img_url', '$source', '$text', '$retweet_count', '$followers_count',
                            '$friends_count', '$listed_count', '$favorites_count')");

        if (!$ok) {
            echo "Mysql Error: " . mysql_error();
        }

        flush();
    }
}
?>

You should try javascript function setInterval . 您应该尝试使用javascript函数setInterval

<script type="text/javascript">
var myVar=setInterval(function(){makeRequest()},5000);
function myStopFunction()
{
    clearInterval(myVar);
}
</script>

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

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