This is a Q&A section where I answer some questions I have been asked.
Dealing With XML
Question: Is there a quick and easy built in function in PHP to convert XML to an array?
In short yet, assuming the XML your trying to sieve through is not too complex the simplexml_load_file() function should be able to handle your needs.
Telling a friend they got ‘had’
Question: A colleague of mine recently went about starting his own .com, unfortunately the Designer/Coder they used was awful (for some reason, they did not ask me as well). Basically they paid (a large sum of money) to have a wordpress blog with a tacky (and invalid) design slapped on top of it. Should I tell them they got ripped off, and if yes how should I go about it?
This is quite a sensitive subject, on the one hand they have been seriously ripped off (unclosed tags is not forgivable), but on the other hand they also may not take too kindly to finding out that their website is about as useful as a burning boat.
I suggest showing your friend his website in another web browser, generally if the coding sucks it should render incorrectly. Hopefully the little nudge in the right direction should set the ball rolling in terms of fixing the problems. If they don’t fix it (or ignore the problem), their loss.
In this quick tutorial I’m going to show you how to make a simple “return all” application for facebook. Before you start take a look at the Facebook Developers page and the Facebook Developers Wiki as it’s full of useful stuff.
The Core Components
API – Lets you talk to the facebook servers. [put picture here explaining in detail]
FBML – Facebook markup language, it’s like a cute little html snippet which facebook turns into normal HTML.
XFBML – A javascript which lets you use FBML in iframes.
FQL – Lets you run SQL type query’s.
FBJS – This changes your javascript so you can only work within a close environment.
PHP Client Libraries – The PHP Facebook provides to communicate with their servers.
Here is a really easy and lightweight way of logging the amount of downloads.
<?php # File created on 25th April 2009 by Mike Rogers (http://www.fullondesign.co.uk/).
## Start defining constants ##
define(LOG_URL, '/home/user/download_logs/'); // Put the location of where you want to put the logs. Make sure this is absolute
# $_GET['ID'] - this is the ID of the file we want. It gets the value from the URL.
## Now set the data you wish to use - this can be moved to an include if you want ##
$file[0] = 'http://www.example.com/file.pdf';
## Define the functions required to update the file.
function update_file($filename, $value){
if(is_writable($filename) && is_readable($filename)){
if(file_put_contents($filename, $value)){
return TRUE;
}
}
return NULL;
}
function pull_file($filename){
if(is_writable($filename) && is_readable($filename)){
return file_get_contents($filename);
}
return NULL;
}
function rebuild_file($filename){
if(is_writable($filename) && is_readable($filename)){
file_put_contents($filename, '1');
}
}
if(is_numeric($_GET['ID'])){ // Meaning the Data sent is safe.
// Build log file URL
$filename = LOG_URL.$_GET['ID'].'.log.txt';
$value = pull_file($filename);
header('location:'.$file[$_GET['ID']]);
// Update logs
if(is_numeric($value)){
update_file($filename, ($value+1));
} else { // Meaning the file does not exist or has been messed with.
rebuild_file($filename);
}
} else {
echo 'Sorry, there was an error.';
}
/* If you want to see how many people have downloaded a file, run something like:
# pull_file(LOG_URL.numberishere.'.log.txt');
/*
You are free to share, modify and use this code for commercial uses. Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
I claim no liability for this code, you use it at your own risk.
*/
?>
You can run this by changing the URL that accesses the file. For example: file.php?ID=0
PHP is lovely, though if you ever work with other people having good code is important. Here are some tips to improve your code.
Comment & Document
It’s really important that people understand why you are doing certain things in certain ways. Adding a quick comment above sections of code should be adequate, but documenting classes and functions (Even if it’s in a Wiki) is fantastic.
Give Variables, Functions and Classes Meaningful names
Nothing is worse than trying to figure out what a function called “SIDFE()” does. Give everything a name that if someone else looked at it, they could figure out what it does. The above is a real example I have come across while adjusting a clients website, if the other programmer had called it something like Scan_Incomming_Data_For_Evil() it would have been a lot more straightforward.
White space
As you can see, a little space here and there makes life a lot easier.
No one likes to have to search for the start of a function. Make sure you indent your code and keep it easy to read quickly.
Never Delete – Comment out
This one is a little hard to grasp, but imagine you just fixed a bug (say 100 lines of code to fix it) and something else has broken. It makes sense to be able to go back and see the old code without modification of the new code. Also, doing this helps people see where an old bug was (assuming you comment that the section of code is evil) for future reference.
Use Braces
Braces are those neat } and { things. If you don’t use them on various functions it’s a pain to figure out where a loop starts and finishes. This is especially important when programming on a large scale because no one likes debugging fugly code. Here is an example of good and bad code:
<?php
/* Examples of annoying code */
if ( $coder === 'Silly' ) bang_head();
while ( $coder === 'Silly' )
bang_head();
/* Examples of good code*/
if ( $coder !== 'Silly' ){ Drink_Beer(); }
while ( $coder !== 'Silly' ){
Drink_Beer();
}
?>
StumbleUpon is a funky web 2.0 community where it recommends websites you may be interested in based on other people similar interests. Unfortunately they do not have an API for easy integration. Luckily though, they do offer RSS Feeds for user’s activity. Here is the code you need to show your recent stumbles:
<?php # File created on 3rd April 2009 by Mike Rogers (http://www.fullondesign.co.uk/).
/*
function - recent_stumbles(string $username [, string $type= NULL [, int $limit = 5]])
$username - The stumbleupon username, such as rogem002
$type - Default: NULL - What you want to limit your rss to show. Can be NULL, blog, comments, favorites or reviews
$limit - Default: 5 - how many tweets you wish to show, must be numeric.
*/
function recent_stumbles($username, $type=NULL, $limit=5){
if(!is_numeric($limit)){$limit = 5;}
if($type !== NULL && $type !== 'blog' && $type !== 'comments' && $type !== 'favorites' && $type !== 'reviews'){$type = NULL;}
$xml = simplexml_load_file('http://rss.stumbleupon.com/user/'.urlencode($username).'/'.$type);
$items_count= count($xml->channel->item);
if($items_count < $limit){$limit = $items_count;}
$i = 0;
$return .= '
<ul>';
while($i < $limit){
$return .= '
<li title="'.$xml->channel->item[$i]->title.'"><!-- '.$xml->channel->item[$i]->pubDate.' -->
<a href="'.$xml->channel->item[$i]->link.'" title="'.$xml->channel->item[$i]->title.'"><img src="'.$xml->channel->item[$i]->enclosure["url"].'" alt="'.$xml->channel->item[$i]->title.'" border="0" />
'.$xml->channel->item[$i]->title.'</a></li>
';
$i++;
}
$return .= '</ul>
';
return $return;
}
echo recent_stumbles('Rogem002', 'favorites', 5);
/*
You are free to share, modify and use this code for commercial uses. Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
*/
?>