Skip to content

Instantly share code, notes, and snippets.

@meeDamian
Created December 20, 2011 08:39
Show Gist options
  • Save meeDamian/1500809 to your computer and use it in GitHub Desktop.
Save meeDamian/1500809 to your computer and use it in GitHub Desktop.
[ PHP | api | google ] goo.gl link shortenter
<?
// add it to config file:
$google_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// add this to libs file:
function shorten($url) {
$gurl = "https://www.googleapis.com/urlshortener/v1/url?key=" . $GLOBALS['google_key'];
$url = json_encode(array("longUrl" => $url));
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $gurl);
curl_setopt($ch, CURLOPT_POSTFIELDS, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: appliaction/json", "Content-Type: application/json"));
$r = json_decode(curl_exec($ch));
return $r->id;
}
// example usage:
$short_url = shorten("https://gist.github.com/gists/1500809");
?>
@ao
Copy link

ao commented Aug 11, 2017

Very cool, but appliaction/json should be application/json

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