显示最新的 Twitter Feed“创建日期"使用 tmhOAuth 和 PHP [英] Display latest Twitter Feed "date created" using tmhOAuth and PHP
问题描述
自从 Twitter API v1 退休"以来,我最终寻找了一种使用 PHP 显示最新 Twitter Feed 的新方法,我找到了themattharris/tmhOAuth"(用 PHP 编写的 OAuth 1.0A 库),可以下载在 GitHub:
Since Twitter API v1 "retired", I ended up looking for a new way to display the latest Twitter Feed using PHP, which I found "themattharris / tmhOAuth" (OAuth 1.0A library written in PHP), which can be downloaded at GitHub:
我检查了谷歌,发现了两个使用上述库的有用链接:
I checked google and found two useful links in using the said library:
- http://www.simplistips.com/tips/advanced/twitter-oauth-using-php-api-version-1-0-1-1/
- 使用 PHP 和新的 Twitter API
多亏了以下链接,我才能显示最新的 Twitter 提要.这是我在 PHP 中的代码:
Thanks to the following links, I'm able to display the latest twitter feed. Here are my codes in PHP:
require '../class/tmhOAuth/tmhOAuth.php';
require '../class/tmhOAuth/tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'xxx',
'consumer_secret' => 'xxx',
'user_token' => 'xxx',
'user_secret' => 'xxx',
));
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => 'xxx',
'count' => '1'));
$response = $tmhOAuth->response['response'];
$twitFeed = json_decode($response, true);
$twitFeed = $twitFeed[0]['text'];
$twitFeed = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $twitFeed);
$twitFeed = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $twitFeed);
$twitFeed = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $twitFeed);
echo($twitFeed);
但我似乎无法显示日期,有人可以帮我吗?我试过foreach"
But I can’t seem to display the date, can anyone help me? I've tried "foreach"
foreach ($twitFeed as $item) {
$feedDate = $item->created_at;
}
但它给了我注意:试图在第 xx 行的 http:localhost/try.php 中获取非对象的属性",它指向$feedDate = $item->created_at;"
But it's giving me "Notice: Trying to get property of non-object in http:localhost/try.php on line xx", which is pointing in the "$feedDate = $item->created_at;"
我对 Twitter API 还是有点陌生...请帮忙...
I'm still kinda new with Twitter API... Please help...
推荐答案
Opps,刚刚在我的困境中找到了解决方案,无论如何我都会将代码分享给任何人...
Opps, just got the solution in my dilemma, I'll be sharing the code to anyone anyways...
<?php
$twitter_id = "your_twitter_id";
$no_of_Feeds = 1;
require "class/tmhOAuth/tmhOAuth.php";
require "class/tmhOAuth/tmhUtilities.php";
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'your_consumer_key',
'consumer_secret' => 'your_consumer_secret',
'user_token' => 'your_user_token',
'user_secret' => 'your_user_secret',
));
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => $twitter_id,
'count' => $no_of_Feeds));
$response = $tmhOAuth->response['response'];
$twitFeed = json_decode($response, true);
foreach ($twitFeed as $latestFeed) {
$feedDate = $latestFeed["created_at"];
$postedLast = explode(" ", $feedDate);
$feedCreatedAt = $postedLast[0]." - ".$postedLast[1]." ".$postedLast[2].", ".$postedLast[5];
$feedMsg = $latestFeed["text"];
$feedMsg = preg_replace("/([\w]+\:\/\/[\w-?&;#~=.\/\@]+[\w\/])/", "<a target="_blank" href="$1">$1</a>", $feedMsg);
$feedMsg = preg_replace("/#([A-Za-z0-9\/.]*)/", "<a target="_new" href="http://twitter.com/search?q=$1">#$1</a>", $feedMsg);
$feedMsg = preg_replace("/@([A-Za-z0-9\/.]*)/", "<a href="http://www.twitter.com/$1">@$1</a>", $feedMsg);
}
?>
<a href="https://twitter.com/<?php echo $twitter_id; ?>" target="_blank">@<?php echo $twitter_id; ?></a> <b>said:</b> <?php echo $feedMsg; ?> <b>last</b> <?php echo $feedCreatedAt; ?><br />
刚刚将 "$item->created_at" 替换为 "$latestFeed["created_at"]".
Just replaced the "$item->created_at" to "$latestFeed["created_at"]".
这篇关于显示最新的 Twitter Feed“创建日期"使用 tmhOAuth 和 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!