Display latest Twitter Feed “date created” using t

2019-05-22 20:18发布

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:

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);

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;      
}

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;"

I'm still kinda new with Twitter API... Please help...

标签: php twitter
1条回答
\"骚年 ilove
2楼-- · 2019-05-22 20:56

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 />

Just replaced the "$item->created_at" to "$latestFeed["created_at"]".

查看更多
登录 后发表回答