Shorten URLs using the Google URL Shortener and PHP
<?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.'" />';
?>

Processing your request, Please wait....
No Responses to “Shorten URLs using the Google URL Shortener and PHP”
Comments (Your Comments)
Leave a Reply