Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Google API PHP Example Google URL Shortener API

PHP Example Google URL Shortener API

What is the URL Shortener API?

The Google URL Shortener at goo.gl is a service that takes long URLs and squeezes them into fewer characters to make a link that is easier to share, tweet, or email to friends.

This tutorial includes 2 main parts: Shorten long URLs and expand shortened URLs.

1. Shorten a long URL: allows you to shorten any long URL, ex: from http://4rapiddev.com/google-api/php-example-google-url-shortener-api/ to http://goo.gl/Ct4Nc

2. Expand a short URL: allows you to expand any short URL, ex: from http://goo.gl/Ct4Nc to http://4rapiddev.com/google-api/php-example-google-url-shortener-api/

Note: for every API call, you should Provide an API key which is highly recommended to get a higher usage limits & traffic analytics report.Getting a key from APIs Console.

Example & demo will be posted later, I’m reviewing their API and will post an example very shortly.

[php] <?php
// Declare the class
class GoogleUrlApi {

// Constructor
function GoogleURLAPI($key,$apiURL = ‘https://www.googleapis.com/urlshortener/v1/url’) {
// Keep the API Url
$this->apiURL = $apiURL.’?key=’.$key;
}

// Shorten a URL
function shorten($url) {
// Send information along
$response = $this->send($url);
// Return the result
return isset($response[‘id’]) ? $response[‘id’] : false;
}

// Expand a URL
function expand($url) {
// Send information along
$response = $this->send($url,false);
// Return the result
return isset($response[‘longUrl’]) ? $response[‘longUrl’] : false;
}

// Send information to Google
function send($url,$shorten = true) {
// Create cURL
$ch = curl_init();
// If we’re shortening a URL…
if($shorten) {
curl_setopt($ch,CURLOPT_URL,$this->apiURL);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
}
else {
curl_setopt($ch,CURLOPT_URL,$this->apiURL.’&shortUrl=’.$url);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// Execute the post
$result = curl_exec($ch);
// Close the connection
curl_close($ch);
// Return the result
return json_decode($result,true);
}
}
// Create instance with key
$key = ‘AIzaSyBhdyvJn6YCG8h-_F5YhoQwtfNLtsHX4Ds’;
$googer = new GoogleURLAPI($key);

// Test: Shorten a URL
$shortDWName = $googer->shorten("http://davidwalsh.name");
echo $shortDWName; // returns http://goo.gl/i002

// Test: Expand a URL
$longDWName = $googer->expand($shortDWName);
echo $longDWName; // returns http://davidwalsh.name

?>
[/php]

Feb 25, 2011Hoan Huynh
Javascript generate a random number using Math.randomLinux remove entire directory
You Might Also Like:
  • PHP CURL Post To HTTPS Website
  • How To Track Website With Multiple Google Analytisc Accounts
  • PHP Get Webpage Content Using cURL
  • Display User Current Location On Google Map
  • Facebook Like Button And Recommend Button With fb:like, iframe and html5
  • Check Google PageRank With PHP
  • Export Google Analytics Report To PDF In New Version
  • Google Map Supported Type And Simple Example
  • How To Send Google Analytics Report To Emails
  • How To Export Google Analytics Report To PDF Or XML
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago Google APIGoogle URL Shortener API, long urls, short urls429
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,209 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,077 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,854 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,334 views
C# Read Json From URL And Parse/Deserialize Json
9,828 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Photo Editor App – How Good Is it?

  • The Best Way To Write An Essay – Creating A Good Essay
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (114)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development