Sunday 21 June 2009

PHP-CLI url shortener

/* I'm starting to learn php.
* Below is modified sample code from u.nu
*
* Example:
*
* Input: kaibathelegacy.blogspot.com
*
* Output: Shortened URL = http://u.nu/3gmd
*
*/

echo "Please enter your URL \n";
$original_url = trim(fgets(STDIN));
$request = 'http://u.nu/unu-api-simple?url='.urlencode($original_url);
$response = file_get_contents($request);
if (substr($request, 0, 4) == 'http')
{
echo "Shortened URL = $response";
}
else
{
list ($error_code, $error_message) = explode('|', $response);
echo "Error = $error_message ($error_code)";
}
?>

Google custom search

Custom Search