Created
June 26, 2012 19:50
-
-
Save bkvirendra/2998422 to your computer and use it in GitHub Desktop.
url Shortner function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// By Virendra Rajput | |
// June 27, 2012 | |
function shortner($url) { | |
$longUrl = $url; // url to be shortned | |
$apiKey = ''; // Get API key from : http://code.google.com/apis/console/ | |
$postData = array('longUrl' => $longUrl, 'key' => $apiKey); | |
$jsonData = json_encode($postData); | |
$curlObj = curl_init(); | |
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url'); | |
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); | |
return $json->id; // return the shortened url | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment