【问题标题】:Twitter API requests every 16 minutesTwitter API 每 16 分钟请求一次
【发布时间】:2013-11-29 22:14:47
【问题描述】:

问题:

我编写了一段 PHP 代码,用于检查数组中的用户名是否存在于 Twitter API 协议中,但我不太清楚如何告诉 PHP 或使用 jQuery 与 PHP 一起检查 180 个用户名每 16分钟(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 个值,我想每 16 分钟检查 180 个值(为了安全起见)并将它们一个接一个地添加。所以 180 次调用,打印结果,等待 16 分钟,180 次调用,直接打印结果,等待 16 分钟,等等。

【问题讨论】:

  • set_time_limitsleep() 浮现在脑海中,如果阻塞不是问题?
  • 我想我需要在某个地方放置一个计数器,但实际上前 180 个调用会被打印出来吗?当我用 date('h:i:s') 尝试 sleep() 时,我在经过 10 秒后首先得到了两次。
  • 除非您使用服务器推送,否则无法使用 PHP 执行此操作。您应该做的是使用 javascript 的 setInteval 创建一个回调,该回调将对您的 PHP 脚本执行 XHRequest (ajax),以便为您检索数据...
  • 这听起来是个好主意,但是我不清楚您如何划分 700 值数组并在间隔之间发送 180 个值?你有例子吗?
  • 如果您在客户端执行此操作,只需将所有值转储到一个数组中,然后通过它循环使用 AJAX 每 5 秒调用一次服务器。如果您在服务器端执行此操作,则可以使用数据库或平面文件(可能是 json?)来存储每个文件的状态 - 效率不是很高,但它会起作用。

标签: php jquery arrays twitter timeout


【解决方案1】:

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

index.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>';
        }

    }
?>

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2023-03-06
    相关资源
    最近更新 更多