Google URL Shortener API Service PHP Script

Posted by & filed under Google, PHP.

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… ——– >

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.



Here is sample PHP script for Google URL Shortener Service.

$msg = '';
$flag = false;
try {
	if (isset($_POST['url']))
		$url = $_POST['url'];
		$referer = '';
		$apiKey = 'YOUR API KEY';
		$post_data = json_encode( array( 'longUrl'=>$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,"".$apiKey);
		curl_setopt($ch, CURLOPT_REFERER, $referer);
		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);
		    throw new Exception(curl_error($ch));
		$short_url = json_decode($output);
			throw new Exception($short_url->error->message);
		$msg = $short_url->id;
		$flag = true;
} catch (Exception $e) {
	$msg = $e->getMessage();
	$flag = true;

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…………..

Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts

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

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

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

Get Updates, Scripts & Other Useful Resources to your Email

Join 10,000+ Happy Subscribers on feedburner. Click to Subscribe (We don't send spam)
Every Email Subsciber could have access to download 100+ demo scripts & all future scripts.

%d bloggers like this:

Get Instant Script Download Access!