Skip to content

Instantly share code, notes, and snippets.

@nebiros
Created April 23, 2014 20:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebiros/11231416 to your computer and use it in GitHub Desktop.
Save nebiros/11231416 to your computer and use it in GitHub Desktop.
URL shortener using google's and CURL.
<?php
define("DEFAULT_URL_SHORTENER_API_URL", "https://www.googleapis.com/urlshortener/v1/url");
function _get_short_url($longUrl) {
$curl = curl_init();
$options = array(
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array("Content-Type: application/json"),
CURLOPT_URL => DEFAULT_URL_SHORTENER_API_URL,
CURLOPT_POSTFIELDS => json_encode(array("longUrl" => $longUrl))
);
curl_setopt_array($curl, $options);
if (false === ($resp = curl_exec($curl))) {
trigger_error("Error " . curl_errno($curl) . ": " . curl_error($curl));
curl_close($curl);
return false;
}
curl_close($curl);
$resp = utf8_encode($resp);
$json = json_decode((string) $resp, true);
return $json["id"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment