Google URL Shortener API Service PHP Script

Google URL Shortener API is one of the Google service, which takes long url ( and squeezes it into short as URL with few characters ( to use in tweets and etc…


Demo ——– >

Google-URL-shortener-api-service-tutorial php script

Google URL Shortener API Service

Steps :

When we are sending request (with long url to make short url) to Google URL Shortener API service, Google will authenticate our application with API key or through OAuth 2 token authentication. So we need either Google API key or Google oAuth 2.0 client ID.

To get Google API key or Google oAuth 2.0 client ID please refer my previous tutorial on How to get Google API key and Google oAuth 2.0 client ID.

download google maps reverse geocode api in php source code free


Here is sample PHP script for Google URL Shortener Service.

if (isset($_POST['url']))
		$ch= curl_init();
		$arr = array();
		array_push($arr, 'Content-Type: application/json; charset=utf-8');
		curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);
		curl_setopt($ch, CURLOPT_URL,"");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// we are doing a POST request
		curl_setopt($ch, CURLOPT_POST, 1);
		// adding the post variables to the request
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$output = curl_exec($ch);
		<div id='output'>
		<p><b>Your Short URL : </b></p>
		<textarea rows="4" cols="50"><?php echo $short_url->id;?></textarea>

	<?php } ?>

1. Get long URL from the user using HTML form using post request method.

2. Here I am using CURL (php library) to send request (with long URL and Google API key) to Google URL Shortener API.

note : CURL is php library that enables us to connect and communicate with the different server (linux server, windows server etc) with different protocols http, https,ftp and etc..

Example request URL :””&key=’AIzaSyCBadfG30NHySfUNP8H1ofsgdrt45fdghd’

note : Don’t use above URL directly, You will bad request error. Please send request to Google URL shortener service using CURL like in the above source code.

3. Google URL Shortener service gives response in the form JSON string.

4. Finally converting the JSON string into JSON object using json_decode() method.

                $output = curl_exec($ch);
                echo $short_url->id;

Please leave your feedback in the comment, if your came across any diffculty………….

Note : Premium users can able to download all the scripts* without any waiting time. So please become premium user.    

If you want any of my script need to be customized according to your business requirement,

Please feel free to contact me [at] muni[at]

Note: But it will be charged based on your customization requirement

* - Except Invoice Script

Get Hottest Updates, Scripts & Other Useful Resources to your Email

  • Ladonna

    Greaqt article, еxactly what I was loߋking

Get Instant Script Download Access!