Archive for November, 2009
Create a TinyURL using PHP and TinyURL API
Posted by vivek in PHP, Web Service on November 24th, 2009
TinyURL is an awesome service. For those who don’t know what TinyURL is, TinyURL allows you to convert your long URLs like “http://www.akchauhan.com/category/jquery/” to small URLs like “http://tinyurl.com/yz4c4ba”. Basically you need such type of service when you are posting your data to some third party web service and there is limit on the number of characters you can post like Twitter. Twitter only allow 140 characters long Tweet.
Using the PHP and TinyURL API, you can create these tiny URLs.
function get_tiny_url($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data = curl_exec($ch); curl_close($ch); return $data; } //test it out! $new_url = get_tiny_url('http://www.akchauhan.com/category/jquery/'); //returns http://tinyurl.com/yz4c4ba echo $new_url;
Create Random Alphanumeric Password in PHP
We can use this simple password generation function to create random passwords. The maximum limit of generated password is 36 characters.
<?php function gen_password($size = 8) { $size = $size > 36 ? 30 : $size; $pool = array_merge(range(0, 9), range('A', 'Z')); $rand_keys = array_rand($pool, $size); $password = ''; foreach ($rand_keys as $key) { $password .= $pool[$key]; } return $password; } $password = gen_password(10); ?>
We can use it like this.
<?php $password = gen_password(10); ?>