Are you new? Read the FAQ and catch up on!
762 views

Shorten a URL using PHP and is.gd

This is a really easy way to shorten URLs with PHP, using services like is.gd:

PHP
  1. /**
  2.      * Shortens a url using the url shortening service "is.gd"
  3.      *
  4.      * @param string $original The original URL
  5.      * @return string the shortened version of the original URL
  6.      */
  7.     static public function shortenUrl($original)
  8.     {
  9.         return file_get_contents("http://is.gd/api.php?longurl=".rawurlencode($original));
  10.     }
  11.  

­

If you want to improve a little bit this function, you probably shoud check if the server responds with a 200 or a 500 code.

Tags: PHP strings url shortening api

Embed: