每 16 分钟请求一次 Twitter API


Twitter API requests every 16 minutes

问题:

我写了一段PHP代码,根据Twitter API协议检查数组中的用户名是否存在,但我不太清楚如何告诉PHP或将jQuery与PHP一起使用每16分钟检查180个用户名(Twitter API的GET限制为每15分钟180次调用)。

任何想法将不胜感激。

PHP代码:

foreach ($wordArray as $key => $value)
{
    echo '
        <p>
            English word: ' . $value['English'] . '<br>
            Latin word: ' . $value['Latin'] . '<br>
        </p>
    ';
    $username       = $value['Latin'];
    $url            = 'https://api.twitter.com/1.1/users/show.json';
    $requestMethod  = 'GET';
    $getfield       = '?screen_name='.$username;
    $twitter        = new TwitterAPIExchange($settings);
    $result         = $twitter->setGetfield($getfield)
                              ->buildOauth($url, $requestMethod)
                              ->performRequest();
    // Decode the JSON results
    $value          = json_decode($result);
    // Compare the resulting screen name with the input variable
    if (strtolower($value->screen_name) == $username)
    {
        echo '<p style="color:red;">The user exists!</p>';
    }
    else
    {
        echo '<p style="color:green;">The user does not exist!</p>';
    }
}

期望的解决方案:

假设$wordArray有 700 个值,我想每 180 分钟检查 16 个值(为了安全起见)并将它们逐个添加。所以180个电话,打印出结果,等待16分钟,180个电话,直接打印结果,等待16分钟,等等。

这是完整的工作解决方案。

索引.html

<!DOCTYPE html>
<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var attemp_count = 0;
            var auto_refresh = setInterval(function() 
            {
                $.ajax({
                    url: 'twuserlookup.php',
                    type: 'POST',
                    data: {attemp_count: attemp_count++}
                }).done(function(data) 
                {
                    $('#mydiv').append(data);
                });
                if(attemp_count > 775) 
                {
                    clearInterval(auto_refresh);
                }
            }, 6000);
        });
    </script>
</head>
<body>
    <div id="mydiv"></div>
</body>
</html>

twuserlookup.php

<?php
    // Initialize session
    session_start();
    // Check if array exist
    if (!isset($_SESSION['Words']))
    {
        // Include API protocol for Twitter and Simple HTML DOM
        require_once "dom/simple_html_dom.php";
        // Create DOM from URL or file
        $html = file_get_html('http://www.latinwordlist.com/latin-word-for/index.php');
        // Go through each word pair
        foreach(($html->find('body', 0)->find('li')) as $wordpair) 
        {
            // Find English words
            $items[]    = array (
                                'English' => strtolower($wordpair->find('a', 0)->innertext), 
                                'Latin' => preg_replace('/'W'w+'s*('W*)$/', '$1', strtolower($wordpair->find('dd', 0)->innertext))
                            );
        }
        $wordArray = array_filter(array_slice($items, 0, 775, true));
        $_SESSION['Words'] = $wordArray;
    }
    // Include API protocol for Twitter and Simple HTML DOM
    require_once "TwitterAPIExchange.php";
    // Apply oAuth Access Token and Consumer Keys from Twitter Dev
    $settings = array(
        'oauth_access_token'        => "",
        'oauth_access_token_secret' => "",
        'consumer_key'              => "",
        'consumer_secret'           => ""
    );
    if (array_key_exists('attemp_count', $_POST) && array_key_exists($_POST['attemp_count'], $_SESSION['Words'])) 
    {       
        echo '
            <p>
                English word: ' . $_SESSION['Words'][$_POST['attemp_count']]['English'] . '<br>
                Latin word: ' . $_SESSION['Words'][$_POST['attemp_count']]['Latin'] . '<br>
            </p>
        ';
        $username       = $_SESSION['Words'][$_POST['attemp_count']]['Latin'];
        $url            = 'https://api.twitter.com/1.1/users/show.json';
        $requestMethod  = 'GET';
        $getfield       = '?screen_name='.$username;
        $twitter        = new TwitterAPIExchange($settings);
        $result         = $twitter->setGetfield($getfield)
                                  ->buildOauth($url, $requestMethod)
                                  ->performRequest();
        // Decode the JSON results
        $value          = json_decode($result);
        // Compare the resulting screen name with the input variable
        if (strtolower($value->screen_name) == $username)
        {
            echo '<p style="color:red;">The user exists!</p>';
        }
        else
        {
            echo '<p style="color:green;">The user does not exist!</p>';
        }
    }
?>