<?php
// functions
function gradientHexBGcolor($k=6){
return sprintf("%02X%02X%02X", 29+$k*18, 9+$k*20,9+$k*20);
}
function gradientHexFGcolor($k=6){
return sprintf("%02X%02X%02X", 260-$k*5, 260-$k*5,260-$k*5);
}
// Init Data
$fill = '';
$thisPage = $_SERVER['REQUEST_URI'];
//Enter your Twitter data here:
$twitName = "MyTwitterUsername";
$twitPass = "MyTwitterPassword";
$twitUserDataURL = "http://twitter.com/users/show/MyTwitterUsername.json";
$twitGetURL = "http://twitter.com/statuses/friends_timeline.json";
// Check if a hashtag was given?
if (!$_POST['hashtag']) {
$fill = 'Enter hashtag here...';
}
else {
$tag = $_POST['hashtag'];
$twitGetURL = "http://search.twitter.com/search.json?q=%23".$tag;
}
// cURL 1: get Userdata:
// Init cURL
$curl = curl_init();
// set Userdata URL
curl_setopt($curl,CURLOPT_URL,$twitUserDataURL);
// prepare for return values
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
// set authentification
curl_setopt($curl,CURLOPT_USERPWD,"$twitName:$twitPass");
// get + decode return values
$returnUser = json_decode(curl_exec($curl));
// close cURL
curl_close($curl);
// cURL 2: get timeline or tag search data:
// Init cURL
$curl = curl_init();
// set URL
curl_setopt($curl,CURLOPT_URL,$twitGetURL);
// prepare for return values
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
// set authentification
curl_setopt($curl,CURLOPT_USERPWD,"$twitName:$twitPass");
// get + decode return values
$return = json_decode(curl_exec($curl));
// close cURL:
curl_close($curl);
// define & output css style definitions:
echo '<style type="text/css">
div, .twitapi a, p, span, table, td, input, textarea, b, blockquote, th, form {font-family:Tahoma,Trebuchet MS,Arial,sans-serif;font-size:8pt;border:0;margin:0;padding:0;line-height:140%;}
div.twitapi {width:540px;background:#CDCDCD;padding:6px 0;}
.twitapi p, .twitapi br {display:none;}
.twitapi hr {border:0;border-top:1px dotted #FFF;}
.twitapi table {#position:relative;border:0;border-collapse:collapse;width:100%;}
.twitapi table td {width:50%;border:0;padding:0;overflow:hidden;}
pre {color:#000;text-decoration:none;border:0;margin:0;padding:0;}
.twitapi a, .twitapi img,.twitapi span,.twitapi div {color:#FFF;text-decoration:none;border:0;margin:0;padding:0;}
div.outer {margin:6px 3%;padding:0;width:94%;}
div.wrap {#position:relative;display:table;height:48px;text-align:left;margin:6px 3%;padding:0;width:94%;}
div.text {#position:absolute;#top:50%;display:table-cell;vertical-align: middle;padding:0;}
.twForm {color:#FFF;padding:0;}
.twForm span {display:block;height:24px;margin:0;padding:0;overflow:hidden;}
.twForm input, button, textarea, select {width:100%;height:100%;border:0;padding:5px;color:#FFF;overflow:hidden;}
.twForm .button {text-align:right;}
.twForm .button:hover {color:#'.gradientHexBGcolor(7).';}
.twForm textarea {font-weight:bold;}
div.inner {#position:relative;#top:-50%;overflow:auto;margin:0 10px;}
a.img {display:inline;float:left;width:48px;height:48px;overflow:hidden;}
a.img img {width:48px;height:48px;border:0;}
</style>';
// output twitter container & headline & buttons
echo '<div class="twitapi">
<div class="wrap" style="background-color:#'.gradientHexBGcolor().';">
<table><tr><td>
<a class="img" href="'.$returnUser->profile_image_url.'"><img src ="'. $returnUser->profile_image_url.'"></a>
<form class="twForm" action="" method="POST"><input name="line" value="" type="hidden">
<span><textarea readonly style="background-color:#'.gradientHexBGcolor(6).';">'.$twitName.'\'s timeline</textarea></span>
<span>
<input class="button" style="background-color:#'.gradientHexBGcolor(2).';" value="Refresh now!" type="submit">
</span>
</form>
</td>
<td>
<form class="twForm" action="'.$thisPage.'" method="POST">
<span>
<input class="text" style="background-color:#'.gradientHexBGcolor(8).';" name="hashtag" value="'.$tag.$fill.'" type="text">
</span>
<span>
<input class="button" style="background-color:#'.gradientHexBGcolor(4).';"value="Search now!" name="submit" type="submit">
</span>
</form>
</td>
</tr></table>
</div>
<hr>';
// output tweets
// output friends' timeline if no hashtag given, else tweets with hashtag
if (!$tag) {
foreach($return as $key=>$tweet) {
if ($key<=10) {
$text = $tweet->text;
// Add links to URLS
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" rel=\"nofollow\">\\0</a>", $tweet->text);
// Add links to Twitterprofiles with @
$text = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'", $text);
// Add links to hashtags #
$text = preg_replace("#(^|[\n ])\#([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://hashtags.org/search?query=\\2\" >#\\2</a>'", $text);
echo '<div class="wrap" style="background-color:#'.gradientHexBGcolor($key).';">
<a class="img" href="http://twitter.com/'. $tweet->user->screen_name.'"><img src ="'. $tweet->user->profile_image_url.'"></a>
<div class="text">
<div class="inner" color="#'.gradientHexFGcolor($key).'">
<a href="http://twitter.com/'. $tweet->user->screen_name.'">'. $tweet->user->screen_name.'</a>:
'.$text.' >>'. $tweet->source.'</div></div></div>';
}
}
}
else {
foreach($return->results as $key=>$tweet) {
if ($key<=10) {
$text = $tweet->text;
// Add links to URLS
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" rel=\"nofollow\">\\0</a>", $tweet->text);
// Add links to Twitterprofiles with @
$text = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'", $text);
// Add links to hashtags #
$text = preg_replace("#(^|[\n ])\#([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://hashtags.org/search?query=\\2\" >#\\2</a>'", $text);
echo '<div class="wrap" style = "background-color:#'.gradientHexBGcolor($key).';">
<a class="img" href="http://twitter.com/'. $tweet->from_user.'"><img src ="'. $tweet->profile_image_url.'"></a>
<div class="text">
<div class="inner" color="'.gradientHexFGcolor($key).'"><a href="http://twitter.com/'. $tweet->from_user.'">'. $tweet->from_user.'</a>:
'.$text.' at '. date('M d,Y',strtotime($tweet->created_at)).'</div></div></div>';
}
}
}
echo '</div>';
?>
Feb 28
Saturday, February 28th, 2009| 7:32 pm | Filed under opensource, software, web.
Leave a Reply