Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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