Php 根据特定参数筛选Twitter API数组。获取和并显示结果
我正在使用提取100条tweet的数组,并过滤结果以仅显示过去60分钟的tweet。我还希望这是过去60分钟内的推文总数。注意:这是140开发人员的代码,我正在调整以实现这一点Php 根据特定参数筛选Twitter API数组。获取和并显示结果,php,if-statement,foreach,Php,If Statement,Foreach,我正在使用提取100条tweet的数组,并过滤结果以仅显示过去60分钟的tweet。我还希望这是过去60分钟内的推文总数。注意:这是140开发人员的代码,我正在调整以实现这一点 <?php // The search terms are passed in the q parameter // search_server.php?q=[search terms] if (!empty($_GET['q'])) { // Remove any hack attempts fro
<?php
// The search terms are passed in the q parameter
// search_server.php?q=[search terms]
if (!empty($_GET['q'])) {
// Remove any hack attempts from input data
$search_terms = htmlspecialchars($_GET['q']);
// Get the application OAuth tokens
require 'app_tokens.php';
// Create an OAuth connection
require 'tmhOAuth.php';
$connection = new tmhOAuth(array(
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'user_token' => $user_token,
'user_secret' => $user_secret
));
// Request the most recent 100 matching tweets
$http_code = $connection->request('GET',$connection->url('1.1/search/tweets'),
array('q' => $search_terms,
'count' => 100,
'lang' => 'en',
'type' => 'recent'));
// Search was successful
if ($http_code == 200) {
// Extract the tweets from the API response
$response = json_decode($connection->response['response'],true);
$tweet_data = $response['statuses'];
// Load the template for tweet display
$tweet_template= file_get_contents('tweet_template.html');
// Load the library of tweet display functions
require 'display_lib.php';
// Create a stream of formatted tweets as HTML
$tweet_stream = '';
$sumArray = array();
foreach($tweet_data as $key=>$tweet) {
$hourcount = $tweet['created_at'];
$deltatime2 = time() - strtotime($hourcount);
if ($deltatime2 < (60 * 60)) {
$deltasum = floor($deltatime2 / 60);
}
// Ignore any retweets
if (isset($tweet['retweeted_status'])) {
continue;
}
if ($deltasum < 60) {
$i++;
// Get a fresh copy of the tweet template
$tweet_html = $tweet_template;
// Insert this tweet into the html
$tweet_html = str_replace('[screen_name]',
$tweet['user']['screen_name'],$tweet_html);
$tweet_html = str_replace('[name]',
$tweet['user']['name'],$tweet_html);
$tweet_html = str_replace('[profile_image_url]',
$tweet['user']['profile_image_url'],$tweet_html);
$tweet_html = str_replace('[tweet_id]',
$tweet['id'],$tweet_html);
$tweet_html = str_replace('[tweet_text]',
linkify($tweet['text']),$tweet_html);
$tweet_html = str_replace('[created_at]',
twitter_time($tweet['created_at']),$tweet_html);
$tweet_html = str_replace('[retweet_count]',
$tweet['retweet_count'],$tweet_html);
// Get time value
$tweet_html = str_replace('[minute_count]',
time_value($tweet['created_at']),$tweet_html);
// Add the HTML for this tweet to the stream
$tweet_stream .= $tweet_html;
}
}
// Pass the tweets HTML back to the Ajax request
print $tweet_stream;
// Handle errors from API request
} else {
if ($http_code == 429) {
print 'Error: Twitter API rate limit reached';
} else {
print 'Error: Twitter was not able to process that search';
}
}
} else {
print 'No search terms found';
}
?>
提前感谢。在您的代码中,我读到了以下内容:
$http_code = $connection->request('GET',$connection->url('1.1/search/tweets'),
array('q' => $search_terms,
'count' => 100,
'lang' => 'en',
'type' => 'recent'));
这意味着您将其设置为“最近”,计数为100。我想它会自动从给定的搜索中获取最近100条推文。
时间限制不是多余的吗