PHP: Google URL Shortener: shorten

The following program shows how to make use of the Google URL shortener API to perform URL shortening. Note that this program doesnot make use of the key. So there is a limit to the number of anonymous URL shortening calls that one can make in a day


/*Base URL of the Service*/
$BASEURL = "";

/*Version of the service*/
$VERSION = "v1";

$SERVICE = "url";

/*Type of the content, e.g. text/xml, application/json*/
$CONTENT_TYPE = "Content-Type: application/json";

/*API name*/
$REQUEST = "longUrl";

function printDetails($hash) {
    foreach($hash as $key => $value){
        if (is_array($value)) {
            print "$key:n";
            foreach($value as $val) {
                if(is_object($val) || is_array($val)){
                     print "$val,";
            print "n";
        else if (!is_object($value)) {
            print "$key: $valuen";
        else {
            print "$key:n";

function get_response($URL, $type, $content) {

    if(!function_exists('curl_init')) {
    die ("Curl PHP package not installedn");

    /*Initializing CURL*/
    $curlHandle = curl_init();

    /*The URL to be downloaded is set*/
    curl_setopt($curlHandle, CURLOPT_URL, $URL);
    curl_setopt($curlHandle, CURLOPT_HEADER, false);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array($type));
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $content);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);

    /*Now execute the CURL, download the URL specified*/
    $response = curl_exec($curlHandle);

    /*Return the response as it is, let the application process it accordingly*/
    return $response;

/*Specify the url to be shortened*/

if (sizeof($argv) != 2) {
  echo "Usage: $argv[0] urln";


print "URL: $urln";
print "URL To be shortened: $argv[1]n";

/*Create the Request*/
$request = array($REQUEST => $argv[1]);

/*Encode the request as a json object*/
$jsonrequest = json_encode($request);

$response = get_response($url, $CONTENT_TYPE, $jsonrequest);

/*Printing the response on to the console*/
if (isset($response)) {
   $dresponse = json_decode($response);
   if (json_last_error() == JSON_ERROR_NONE){
       if (isset($dresponse->{'id'})) {
           print "The Shortened URL is: ". $dresponse->{'id'}."n";
           print "======================================================n";
           print "More Details:n";
           print "======================================================n";
           print "======================================================n";
else {
   print "Failed to get a responsen";

Let’s execute the above program. It takes one argument: the name of the URL to be shortened

$ php shorten.php
URL To be shortened:
The Shortened URL is:
More Details:
kind: urlshortener#url

Thus with the above program, you can shorten any URL of your choice. Preferably, you should put the URL to be shortened in quotes when you give the input as parameter to the above ‘shorten’ program


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s