Google URL Shortener API Service PHP Tutorial

Posted by & filed under Google, HTML5, JQUERY, PHP.

In this tutorial I am going to update my previous tutorial on Google URL Shortener API Service PHP with some error handling. Here is my previous tutorial PHP Google URL Shortener API Service.

 

 

PHP Google URL Shortener API Service

Create Google API key for Google URL Shortener API Service

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.

 

 

Google URL Shortener API Service HTML Form

Create HTML form to get long URL from user. Here is my html form.

<form method="post">
	<div class="form-group">
		<label>Enter your URL</label>
		<textarea class="form-control" rows="5" name="url" id="url"></textarea>
		<span class="help-block has-error" id="url_error"></span>
	</div>
	<div class="form-group">
		<button type="submit" id="short_url_btn" data-loading-text="Getting Short URL..." class="btn btn-success">Get Short URL</button>
	</div>
</form>

Google URL Shortener API Service PHP Script

Here is my updated PHP curl script which makes api request to Google URL Shortener API Service, that validates our request and sends the response.
Please replace your API key in the place of ‘YOUR API KEY’.

$msg = '';
$flag = false;
try {
	if (isset($_POST['url']))
	{
		$url = $_POST['url'];
		$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_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;
}
?>

While generating your api key, if you set any referrer like this in the below image.

google-url-shortener-api-service-Referrer

Then you must add referrer in your API curl request like this.

curl_setopt($ch, CURLOPT_REFERER, 'http://www.smarttutorials.net/');

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]gmail.com

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.

Get Instant Script Download Access!