Skip to content

Instantly share code, notes, and snippets.

@kafene
Last active December 11, 2015 17:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kafene/4633973 to your computer and use it in GitHub Desktop.
Save kafene/4633973 to your computer and use it in GitHub Desktop.
<?php
/* Shorten a URL using Google's goo.gl API. Requires an API key. */
function googl_shorten_url($url, $api_key) {
$endpoint = 'https://www.googleapis.com/urlshortener/v1';
$ch = curl_init(sprintf('%s/url?key=%s', $endpoint, $api_key));
curl_setopt_array($ch, array(
\CURLOPT_POST => true
, \CURLOPT_AUTOREFERER => true
, \CURLOPT_FOLLOWLOCATION => true
, \CURLOPT_SSL_VERIFYPEER => false
, \CURLOPT_RETURNTRANSFER => true
, \CURLOPT_HTTPHEADER => array('Content-Type: application/json')
, \CURLOPT_POSTFIELDS => json_encode(array('longUrl' => $url)))
);
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment