Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.