Return to Snippet

Revision: 47890
at June 18, 2011 00:46 by carbonr


Initial Code
// pass the url you want to shorten in $url when calling the fucntion
// second arugument return the qr code url of the shortened goo.gl url
// getgool("http://www.thisisthelongurltoshorten.com", TRUE);

function getgoogl($url, $qr=NULL){
	if(function_exists('curl_init')){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL, 'http://goo.gl/api/shorten');
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'security_token=null&url='.urlencode($url));

		$results = curl_exec($ch);
		$headerInfo = curl_getinfo($ch);
		curl_close($ch);

		if ($headerInfo['http_code'] === 201){ // HTTP Code 201 = Created
			$results = json_decode($results);
			if(isset($results->short_url)){
				$qr = !is_null($qr)?'.qr':'';
				return $results->short_url.$qr;
			}
			return FALSE;
		}
		return FALSE;	

	}
	trigger_error("cURL required to shorten URLs.", E_USER_WARNING); // Show the user a neat error.
	return FALSE;
}

Initial URL


Initial Description


Initial Title
goo.gl URL shorten using Un-traditional API - v1

Initial Tags
url, google

Initial Language
PHP