I have now, finally, succeeded to get into working with the twitter API - one of the most popular around. I am impressed by the possibilities and will try more. This is a little script that I startes following an introduction printed in German PHP Magazin.
The layout needs some finetuning, but it works really well. I will keep you updated when I modify it.
This post might be interesting for you...

  • if you are an beginner or intermediate php developer looking for a base where to start coding your own twitter display.
  • if you are already experienced in copy & pasting php code into your wordpress blog, for example, expanding the functionality of your sidebar or your page template

The Twitter API provides JSON technology and can be accessed by the cURL command. The PHP version you therefore need is 5.2 and above.
I you like to work with this code, copy and paste it into a page on your blog or other php enabled website.
Caution: You need to have the Exec-PHP plugin installed in order for the code to be executed. The additional CSS definitions might scramble the display of your website layout, so consider to modify it if needed.
Does anyone know a workaround for this?
To get the plain php code, click on "view plain" above the code display.


Notice: Twitter only allows 100 updates per hour, so the timeline display might be empty during heavy traffic.
Now have fun with the code:

<?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>';
?>
Tags: