问题:
我编写了一段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分钟等。
答案 0 :(得分:0)
这是完整的解决方案。
<强>的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>';
}
}
?>