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.
Posted October 1st, 2010
When I started learning PHP (Back in the PHP4 days) validating data was always a pain (for me at least). Most of the resources available cited the POSIX functions as the most effective way of validating an email address or URL.
Thankfully since then, the PHP community has embraced the PCRE functions which are more efficient and are Perl-compatible. However the downside to PCRE (and POSIX for that matter) is that you need to know regular expressions, which for a newbie to learn can feel like walking through a minefield.
Recently though the Filter Functions have become a very popular method to validate data. This is due to their small learning curve.
How to use the Filter Functions
In this example (Using the filter_var() function) the filter function takes the data you input (For example: email@example.com) and will return either the data (if it’s valid) or false (if the data is not valid).
Read the rest of this entry »
Posted June 21st, 2010
Here is a function I use from time to time in my code which I thought I should share. It allows you to quickly shorten URL’s via 6 popular URL shortening websites.
function shorten_url($url, $service=NULL){
if($service== 'tinyurl'){return get_link('http://tinyurl.com/api-create.php?url='.urlencode($url));}
elseif($service == 'urly'){return get_link('http://ur.ly/new.txt?href='.urlencode($url));}
elseif($service == 'isgd'){return get_link('http://is.gd/api.php?longurl='.urlencode($url));}
elseif($service == 'klam'){return get_link('http://kl.am/api/shorten/?format=text&url='.urlencode($url));}
elseif($service == 'unu'){return get_link('http://u.nu/unu-api-simple?url='.urlencode($url));}
else{return get_link('http://api.tr.im/v1/trim_simple?url='.urlencode($url));}
return $url;
}
function get_link($url){
if(function_exists('curl_init')){
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$resultArray = curl_getinfo($ch);
if ($resultArray['http_code'] == 200){
return $result;
} else {
return FALSE;
}
curl_close($ch);
}elseif(ini_get('allow_url_fopen') == 1){
return fopen($url, 'r');
}
return FALSE;
}
/* Usage */
echo shorten_url('http://www.fullondesign.co.uk/');
// returns: http://tr.im/JPCz
Posted January 8th, 2010
One functions I wish I had known about when i start learned PHP is the realpath() function. Pretty much what it does is take symbolic link (such as ‘../’ or ‘/’) and returns a canonicalized absolute link (like ‘/home/public_html/folder’). Here is an example of it in action:
<?php
echo realpath('../');
// Would return: /home/mike/www/blogposts
?>
Posted December 19th, 2009
I was recently on YouTube and I noticed that did not check the shortened strings for potentially embarrassing shortenings of words.
How to make a great example?
Here is the code so you can shorten strings and check for potentially embrassing words.
Read the rest of this entry »
Posted June 16th, 2009