0

Twitter ファイアホースを利用して、分析目的で設定されたフィルターに基づいていくつかのツイートを保存しています。

Twitter ストリームへの接続を開き、それを開いたままにしておく PHP スクリプトを作成しました (!eof) ... したがって、これは基本的に無期限に開いたままになります。

ただし、5秒後に停止するように設定しています。AJAX を使用してこのスクリプトを 5.5 秒 (衝突しないようにするためのオフセット) 呼び出してから、成功時に再ループしたいなど...

問題は、関数が「成功」シグナルを受信して​​いないように見えることです。何が起きてる?

私のコードの関連部分は次のとおりです。

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

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

スクリプト:

<?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();
    }
}
?>
4

1 に答える 1

-1

JavaScript 関数setIntervalを試してください。

<script type="text/javascript">
var myVar=setInterval(function(){makeRequest()},5000);
function myStopFunction()
{
    clearInterval(myVar);
}
</script>
于 2013-03-06T17:39:08.030 に答える