Skip to content

Instantly share code, notes, and snippets.

@razan-rai
Last active January 26, 2017 06:17
Show Gist options
  • Select an option

  • Save razan-rai/d739eddbd76b90656918a642c8c0f6a2 to your computer and use it in GitHub Desktop.

Select an option

Save razan-rai/d739eddbd76b90656918a642c8c0f6a2 to your computer and use it in GitHub Desktop.
//example demonstrate generating short url from full url
public function getShortUrl($long_Url)
{
$apiKey="AIzaSyByZhZ9RVU3v-ImcTcvAOVqYIEvPeQ4V5wff";
$data = array('longUrl' => $long_Url, 'key' => $apiKey);
$jsonData = json_encode($data);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey);
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, $jsonData);
$response = curl_exec($curlObj);
// Change the response json string to object
$json = json_decode($response);
curl_close($curlObj);
// echo 'Your Short URL is: '.$json->id;
return $json->id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment