Skip Navagation

Full On Design

A Web Development & Technology Blog

 

PHP

Shorten URLs using the Google URL Shortener and PHP

A few days ago Google opened up their URL shortening service goo.gl to the public. Luckily it is pretty easy to make use of the API and shorten URL’s in PHP. Here is a function I put together which you can make use of.

<?php
// Coded by Mike Rogers (http://www.fullondesign.co.uk/) 1st October 2010.

function shorten($url, $qr=NULL){
	if(function_exists('curl_init')){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL, 'http://goo.gl/api/shorten');
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'security_token=null&url='.urlencode($url));

		$results = curl_exec($ch);
		$headerInfo = curl_getinfo($ch);
		curl_close($ch);

		if ($headerInfo['http_code'] === 201){ // HTTP Code 201 = Created
			$results = json_decode($results);
			if(isset($results->short_url)){
				$qr = !is_null($qr)?'.qr':'';
				return $results->short_url.$qr;
			}
			return FALSE;
		}
		return FALSE;	

	}
	trigger_error("cURL required to shorten URLs.", E_USER_WARNING); // Show the user a neat error.
	return FALSE;
}

// Example: Just the Short URL
echo shorten('http://www.google.com/');

// Example: Give the Short Code URL and image it.
$qrURL = shorten('http://www.google.com/', TRUE);
echo '<img src="'.$qrURL.'" />';
?>

If you want to read up a bit more about the goo.gl shortener, take a look at Matt Cutts post on his blog.

Edit – Added QR Support.

5 changes I would like to see in 2010

2009 has been a somewhat interesting year for the internet; it’s become more mobile & more user friendly. However here are a few ideas I have had over the year, which I would like to see arrive in 2010.

Video Stream Caching

One of the big annoyances I have with streaming video files (normally on websites such as YouTube & Megavideo) is whenever I go to jump to another section of the video I lose what I currently have already loaded. It would be really useful if I could quickly go back to the section I’ve previously loaded.

video_stream_caching
This is a mock up I done, the video I used is from RayWilliamJohnson

I’m quite sure this would need to be implemented by the software, but I can’t see how it would be difficult to add.

Read the rest of this entry »

Handling Errors In PHP

Handling errors in PHP can be quite a handful at times. Here is a really simple PHP class which I use to manage errors:

<?php
/*
errors class - Helps management of errors in a script.

@version
	1.0
@author
	Mike Rogers (FullOnDesin.co.uk)
@last updated
	03 June 2009
@usage
	You are free to share, modify and use this code for commercial or non-commercial uses.
	Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
	You use this at your own risk.
*/
class errors {
	var $errors_data;

	/*
	Add the error from $new_error into an array of errors.

	@param
		$new_error	string The text related to your error.
	@return:
		True - Error has been Added
	@example
		add_error('Username is Incorrect');
	*/
	public function add_error($new_error){
		$this->errors_data[] = $new_error;
		return TRUE;
	}

	/*
		Outputs the errors.

	@param
		None
	@return:
		- A div (ID - error) which contains the errors.
		NULL - No errors
	@example
		echo output_errors();
	*/
	public function output_errors(){
		if(is_array($this->errors_data)){
			// Cycle through the errors.
			foreach($this->errors_data as $error)	{
				$return .= '<p>'.$error.'</p>';
			}
		// Add it to the error div
		return '<div id="error">'.$return.'</div>';
		}
		return NULL;
	}
}

// @Example - creating the class:
$errors = new errors;

// @Example - Add an error
$errors->add_error('Username is incorrect');

// @Example - Return the errors
echo $errors->output_errors();
?>

If you are looking to fully integrate a script similar to the above, there is a really good post regarding the set_error_handler() function on Tinsology ( PHP Error Handling ).

Making a simple Facebook Application

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.

Read the rest of this entry »

Simple PHP Download Counter

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

Feel free to edit and share this code.