Create shortcut links using Google API
I needed a short link, so I made it PHP.
You can use it after registering Google API.
How to use: Shortur_google ('original address', 'API key');
<?php
function shorturl_google($url, $key)
{
$post = array('longUrl' => $url);
$json = json_encode($post);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$key);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $json);
$json = json_decode(curl_exec($curlObj));
curl_close($curlObj);
return $json->id;
}
echo shorturl_google('http://kalmuri.kilho.net/', 'API-KEY-API-KEY-API-KEY');
?>
Leave a Reply