PHP: Google URL Shortener (With Authorization Key): expand

Google provides URL shortening service along with an API. In one of our previous post, we discussed how to make anonymous calls to the service, especailly for shortening and expanding. But anonymous calls has certain limits, you can not make a large number of calls in a single day. So you can make use of the authorization key. If you haven’t got one, get one from here.

Let’s see the program for URL expansion.

Note that you must substitute YOUR_KEY_HERE with your own authorization key


/*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 = "shortUrl";

/*Authorization key*/

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) {

    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_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 expanded*/

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


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

$response = get_response($url."?key=$KEY&$REQUEST=$argv[1]", $CONTENT_TYPE);

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

Let’s execute the program

$ php expand.php

URL To be expanded:
The Expanded URL is:
More Details:
kind: urlshortener#url
status: OK

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