Skip to content

Instantly share code, notes, and snippets.

@skopp
Created June 27, 2013 22:43
Show Gist options
  • Save skopp/5881069 to your computer and use it in GitHub Desktop.
Save skopp/5881069 to your computer and use it in GitHub Desktop.
<?php
$longUrl = 'http://www.your-log-url-here.......';
$apiKey = 'Your Google Api Key here';
//You can get API key here : 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');
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);
$json = json_decode($response);
curl_close($curlObj);
echo 'Shortened URL ->'.$json->id;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment