Archive for June 2009
Embed twitter in webpage with links
My education board decided to block twitter so I got a hold of some code for our school website to allow students to see our school’s tweets.
The code is standard pear-twitter stuff, the cleverness is converting the twitter text url string to a real url.
<?php
// include class
require_once(‘Services/Twitter.php’);
try {
// initialize service object
$service = new Services_Twitter(‘user’, ‘password’);
// get status updates posted by logged-in user and friends
$response = $service->statuses->friends_timeline();
if (count($response->status) > 0) {
echo ‘<h2>Recent status updates</h2>’;
foreach ($response->status as $status) {
echo ‘<div><img src=”‘ . $status->user->profile_image_url . ‘” />’;
$a=preg_replace(“/(http:\/\/|www|[a-zA-Z0-9-]+\.|[a-zA-Z0-9\.-]+@)(([a-zA-Z0-9-][a-zA-Z0-9-]+\.)+[a-zA-Z0-9-\.\/\_\?\%\#\&\=\;\~\!\(\)]+)/”,”<a
href=\”\\1\\2\”>\\1\\2</a>”,$status->text);
echo $a . ‘<br/>’;
echo ‘By: <em>’ . $status->user->screen_name . ‘</em> on ‘ . $status->created_at . ‘</div>’;
}
}
// get recent posts on public timeline
// $response = $service->statuses->public_timeline();
// if (count($response->status) > 0) {
// echo ‘<h2>Recent public timeline updates</h2>’;
// foreach ($response->status as $status) {
// echo ‘<div><img src=”‘ . $status->user->profile_image_url . ‘” />’;
// echo $status->text . ‘<br/>’;
// echo ‘By: <em>’ . $status->user->screen_name . ‘</em> on ‘ . $status->created_at . ‘</div>’;
// }
// }
// perform logout
$service->account->end_session();
} catch (Exception $e) {
die(‘ERROR: ‘ . $e->getMessage());
}
?>