Code snippet: tinyurl link creation api

September 29th, 2008 | Categories: PHP

Here is a quick code snippet to use the tinyurl link creation api.

< ?php
//tinyURL function, needs url to be passed to tinyrul
function tinyURL( $url ) {
	//Initializes a new session and return a cURL handle
	$curl = curl_init( );
	//Sets an option on the given cURL session handle.
	//request url
	curl_setopt( $curl, CURLOPT_URL, "http://tinyurl.com/api-create.php?url=" . urlencode( $url ) );
	curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 ); //return transfer
	curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 10 ); // connection timeout
	//Execute the given cURL session.
	$tiny = curl_exec( $curl );
	//Closes a cURL session and frees all resources. The cURL handle, $curl , is also deleted.
	curl_close( $curl );
	//return result from curl
	return( $tiny );
}

echo tinyURL( "http://strategicv.com/" ); //echos: http://tinyurl.com/3r4j8c
?>

Comments

TOP