Though I had been putting it off since twitter blocked it's original API access logon scheme in favor of OAuth as of 31 August 2010, I knew this needs to be resolved sooner or later due to considerable network traffic in form of tweets, and this is likely to be integral part of this website. This post is intended for whoever interested in publishing their tweets on own pages, fetched in strings using OAuth API. While there may be whole variety of alternatives to offer a solution, mine is rather straight forward and implementable within a single file, while makes all references to an external file 'tmhOAuth.php', which is available in themattharris' OAuth library. So here it goes:
< ? p h p require 'tmhOAuth.php'; // spaces were inserted between the word php to avoid in-page execution
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'xxxxxxxxxxxx',
'consumer_secret' => 'xxxxxxxxxxxxxxxxxxxxxx',
'user_token' => 'xxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxx',
'user_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
)); //twitter OAuth info. to be defined here
$tmhOAuth->request('GET', $tmhOAuth->url('1/statuses/user_timeline')
);
if ($tmhOAuth->response['code'] == 200) {
$content=json_decode($tmhOAuth->response['response']);
} else {
$tmhOAuth->pr(htmlentities($tmhOAuth->response['response']));
} // fetch user's timeline
$counter == 0;
foreach($content as $entry)
{
if (++$counter == 10) {
break;
} //extracts each twitter, adjust $counter depending on how many tweets are to be displayed
$tweet = $entry->text;
$time = $entry->created_at;
$location = $entry->place->full_name;
$created_monthday = substr($time, 3, 8);
$created_time = substr($time, 10, 6);
$created_year = substr($time, 26, 4);
$time_informat = $created_time . $created_monthday . $created_year;
/* the rest is pretty much about what you want to display. At the start print the whole $entry and see what you would like to extract from the string. -Hoonio*/
if(!is_null($location)){
$time_informat= $time_informat . " at " . $location;
}
$print_line="{tweet} {time_informat}
";
$print_line=str_replace("{tweet}",$tweet,$print_line);
$print_line=str_replace("{time_informat}",$time_informat,$print_line);
echo $print_line;
}
error_reporting(E_ALL&(~E_NOTICE));
?>
I’ve read some good stuff here. Definitely worth bookmarking for revisiting.
ReplyDeleteMy blog is about what causes acne