Site icon SmartTutorials.net

Google URL Shortener API Service PHP Script

Google URL Shortener API is one of the Google service, which takes long url (http://smarttutorials.net/demo/google_url_shortener.php) and squeezes it into short as URL with few characters (http://goo.gl/EGA5Y) to use in tweets and etc…

 

 

http://smarttutorials.net/demo/google_url_shortener.php ——– >http://goo.gl/EGA5Y

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.

<?php
$msg = '';
$flag = false;
try {
	if (isset($_POST['url']))
	{
		$url = $_POST['url'];
		$referer = 'http://www.smarttutorials.net/';
		$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,"https://www.googleapis.com/urlshortener/v1/url?key=".$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);
		
		if(curl_errno($ch))
		{
		    throw new Exception(curl_error($ch));
		}
		$short_url = json_decode($output);
		if(isset($short_url->error)){
			throw new Exception($short_url->error->message);
		}
		$msg = $short_url->id;
		$flag = true;
		curl_close($ch);
		
	}	
} 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 :

https://www.googleapis.com/urlshortener/v1/url?longUrl=”http://smarttutorials.net/demo/google_url_shortener.php”&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);
		curl_close($ch);
		$short_url=json_decode($output);
                echo $short_url->id;

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

Exit mobile version