Skip to content

Instantly share code, notes, and snippets.

@0xnbk
Created October 7, 2010 03:38
Show Gist options
  • Save 0xnbk/614512 to your computer and use it in GitHub Desktop.
Save 0xnbk/614512 to your computer and use it in GitHub Desktop.
PHP code to query goo.gl to shorten an url ( your php api for google url shortening )
<?php
// uses Google's http://goo.gl/ URL shortener
/* Enter your url below */
$url = "aroundtheweb.info";
/* Function to get the short url */
function get_short_url($url) {
$ch = curl_init("http://goo.gl/api/url?url=" . urlencode($url));
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($result = curl_exec($ch))
{
$json = json_decode($result);
if($error = ($json->error_message))
{
echo "$error";
exit;
}
$short_url = $json->short_url;
return "$short_url";
} else
{
echo curl_error($ch);
}
}
echo "Url: $url\n";
echo "Shortened url by goo.gl: ".get_short_url($url);
?>
@prawatt
Copy link

prawatt commented Mar 22, 2018

hi nikhil :)

please provide full code of shorter url..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment