Skip to content

Instantly share code, notes, and snippets.

@imammubin
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imammubin/8910132 to your computer and use it in GitHub Desktop.
Save imammubin/8910132 to your computer and use it in GitHub Desktop.
<?php
function GoogleURLShorterner($url)
{
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlHandle, CURLOPT_HEADER, 0);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, '{"longUrl":"'.$url.'"}');
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandle, CURLOPT_TIMEOUT,30);
curl_setopt($curlHandle, CURLOPT_POST, 1);
$content = curl_exec($curlHandle);
$data = json_decode($content);
return $data->id;
}
?>
/*
example of use:
echo GoogleURLShorterner($url);
*/
/*
<a href="http://bit.ly/google-URL-shortener">article</a>
<a href="http://bit.ly/demo-google-URL-shortener">Demo</a>
<a href="http://drive.google.com/uc?export=view&id=0BzdOteXllT06cjdyTUwtZE5Kb00">download</a>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment