Skip to content

Instantly share code, notes, and snippets.

@thelebster
Created February 22, 2013 08:09
Show Gist options
  • Select an option

  • Save thelebster/5011671 to your computer and use it in GitHub Desktop.

Select an option

Save thelebster/5011671 to your computer and use it in GitHub Desktop.
<?php
/**
* Google Url Shortener http://goo.gl/xxxxx
*/
function MYMODULE_urlshortener_get_short_url($url) {
$api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$api_url = 'https://www.googleapis.com/urlshortener/v1/url/?key=' . $api_key;
$post = json_encode(array("longUrl" => $url));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
return $result->id;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment