Return to Snippet

Revision: 47892
at June 18, 2011 00:48 by carbonr


Initial Code
function getgoogl($longUrl){

//This is the URL you want to shorten
$apiKey = 'AIzaSyDMgK9gpIIokl7ClMqMXHZQ7wv_xzRwMHs';
//Get API key from : http://code.google.com/apis/console/
 
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
 
$curlObj = curl_init();
 
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
 
$response = curl_exec($curlObj);
 
//change the response json string to object
$json = json_decode($response);
 
curl_close($curlObj);
 
return $json->id;

}

// call function getgoogl("http://www.longurlhere.com");

Initial URL

                                

Initial Description

                                

Initial Title
goo.gl URL shorten using API & API key - v2

Initial Tags
url, google

Initial Language
PHP