Twitter PHP and SimpleXML feed

Just like the Google Charts API the Twitter API can be super simple. Twitter uses a few different XML feeds to send and receive data from a given user, some feeds require a user name and password supplied through Curl but a few are publicly available to all.

This example will use the built in SimpleXML functions which come with PHP5, this is a much faster and easier way to using alternative readers such as simplePie or anything else you might come across. As far as it goes I don’t think SimpleXML gets enough coverage so I am happy to put it to some use for this example.

Keeping it basic

<?php

$xml = simplexml_load_file(“http://twitter.com/statuses/user_timeline/adi182.xml?count=2″);
if($xml){
echo ‘<h3><a href=”http://www.twitter.com/adi182″>Latest Tweets</a></h3>
<ul>’;
foreach($xml->status as $tweet){
$description = $tweet->text;
echo ‘<li>’.$description.’</li>’;
}
echo ‘ </ul>’;
}
?>

So there you have it, the super simple example on putting a Twiter feed on your website.

Google Charts Example

API’s are becoming more popular on the web as many of the big online companies such as Google, Yahoo and Facebook offer there own open source code for web developers to use freely and easily.

Google Charts Basics

To begin with I am going to go through how to use the Charts Basics API from Google. This is an extremely easy one to start off with as you don’t require an API key or to install anything, you don’t even need a dynamic server side script to get some nice results.

For this example I am going to jazz it up with a bit of PHP using an associative array but you can easily replace this with a database or XML feed.

To call a chart we use the query string linking to the API which will end up looking something like this.

<img src="http://chart.apis.google.com/chart?chs=250x100&amp;chd=t:10,80,35,10&amp;cht=lc&amp;chl=wk1|wk2|wk3|wk4"
alt="Sample chart" />

To generate this dynamicaly I used the following code

$arr = array();

$arr['Jan']['wk1'] = 10;
$arr['Jan']['wk2'] = 80;
$arr['Jan']['wk3'] = 35;
$arr['Jan']['wk4'] = 10;

$chart= 'http://chart.apis.google.com/chart?';
$chartsize = 'chs=250x100';
$type = "cht=lc";
$vals = 'chd=t:';
$keys = 'chl=';
$alt = "January";

foreach( $arr['Jan'] as $key => $value){
	$vals .= $value.',';
	$keys .= $key.'|';
	}

And Lastly to pull all the code together.

<img src="<? echo $chart;?>
<? echo $chartsize;?>
&amp;<? echo substr("$vals", 0, -1);?>
&amp;<? echo $type;?>
&amp;<? echo substr("$keys", 0, -1);?>"
alt="Sample chart" />

To develop this example further use the official documentation available here Google Chart Basic

Random Banner with PHP

This is a quick tutorial which shows how easy it is to make a random banner script with PHP. We will use a PHP array of images which is populated by a given directory and select a random index from the array to be the banner.

For this example I have chosen to list the files from a directory as this will keep the script fairly dynamic without the need of a database. The script could be easily modified to use a database but we can come back to that later on if we need to.

To start off all you will need is a folder with several banners and a page to display them on.

$dir will be the directory you have created to grab the images from, it is important to make sure this folder only contains banners.
$banners will be the array of images found in the given folder.


 $banners = array();
  $dir = 'res/banners/';
  if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
  if($file != "." && $file != ".."){
  $banners[] = $file;
  }
  }
  closedir($handle);
  }

  $rand = array_rand($banners,1);
  echo "<img src='".$dir.$banners[$rand]."' alt='banner'/>";   

The code above uses a few handy built in PHP functions:
opendir()
readdir()
array_rand()
print_r()