2

由于 Twitter API v1 “退休”,我最终寻找一种使用 PHP 显示最新 Twitter Feed 的新方法,我找到了“themattharris / tmhOAuth”(用 PHP 编写的 OAuth 1.0A 库),可以在 GitHub 下载:

我检查了谷歌,发现使用上述库的两个有用链接:

感谢以下链接,我能够显示最新的 Twitter 提要。这是我在 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”

foreach ($twitFeed as $item) {
$feedDate = $item->created_at;      
}

但它给了我“注意:试图在第 xx 行的 http:localhost/try.php 中获取非对象的属性”,它指向“$feedDate = $item->created_at;”

我对 Twitter API 还是有点陌生​​...请帮助...

4

1 回答 1

2

哎呀,刚刚在我的困境中找到了解决方案,无论如何我都会将代码分享给任何人......

<?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"]"。

于 2013-06-16T11:45:28.673 回答