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…

Demo

Demo

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

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

Download

Here is sample PHP script for Google URL Shortener Service.

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.

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

Posted by:

muni

1 Comment

  1. Ladonna -  June 23, 2014 - 8:05 pm

    Greaqt article, еxactly what I was loߋking
    fοr.

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked (required):

Related articles

Back to Top