我是php和Twitter API的新手,下面的代码在没有任何媒体附加到推文的情况下获取推文它只显示链接和主题标签以及可点击的用户名。
我只是希望能够在应用程序上显示推文的照片/图像以及搜索查询结果。
下面的文件包含我正在使用的PHP代码:
displaylip.php:
<?php function linkify($text) {
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"<a href=\"\\0\">\\0</a>", $text);
$text = preg_replace("/\B@(\w+(?!\/))\b/i", '<a href="https://twitter.com/\\1">@\\1</a>', $text);
$text = preg_replace("/\B(?<![=\/])#([\w]+[a-z]+([0-9]+)?)/i", '<a href="https://twitter.com/#!/search/%23\\1">#\\1</a>', $text);
return $text;
}
function twitter_time($time) {
$delta = time() - strtotime($time);
if ($delta < 60) {
return 'less than a minute ago';}
else if ($delta < 120) {
return 'about a minute ago';}
else if ($delta < (60 * 60)) {
return floor($delta / 60) . ' minutes ago';}
else if ($delta < (120 * 60)) {
return 'about an hour ago';}
else if ($delta < (24 * 60 * 60)) {
return floor($delta / 3600) . ' hours ago';}
else if ($delta < (48 * 60 * 60)) {
return '1 day ago';}
else {
return number_format(floor($delta / 86400)) . 'days ago';}
}?>
<?php if (!empty($_GET['q'])) {
$search_terms = htmlspecialchars($_GET['q']);
require 'app_tokens.php';
require 'tmhOAuth.php';
$connection = new tmhOAuth(array(
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'user_token' => $user_token,
'user_secret' => $user_secret
));
$http_code = $connection->request('GET',$connection->url('1.1/search/tweets/update_with_media'),
array('q' => $search_terms,
'count' => 100,
'lang' => 'en',
'include_entities' => 'true',
'type' => 'recent'));
if ($http_code == 200) {
$response = json_decode($connection->response['response'],true);
$tweet_data = $response['statuses'];
$tweet_template= file_get_contents('tweet_template.html');
$tweet_template= file_get_contents('time_line.html');
require 'display_lib.php';
$tweet_stream = '';
foreach($tweet_data as $tweet) {
if (isset($tweet['retweeted_status'])) {
continue;
}
$tweet_html = $tweet_template;
$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('[profile_image_url]',
$tweet['user']['profile_image_url'],$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);
$tweet_stream .= $tweet_html;
}
echo $tweet_stream;
} 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'; } ?>