PHP Script To Shorten URLs Using

AttachmentSize KB

We all know about the URL shortening service offered by many websites. Most notable among them is There are also other free URL shortening services available on the Internet.

In this post, I will show you how to generate a short URL link to any URL from a PHP script using the URL shortening service. The actual code is only about ten lines. We will make use of the Zend_Http_Client component of the Zend Framework.

Let's begin coding. Create the file Shorten.php and paste the below lines of code to it.

Require_once the Zend_Http_Client.php file in your script.

 * @file Shorten.php
 * We assume you have copied the library/Zend directory to the current working directory
 * Make sure you include the correct path
require_once 'Zend/Http/Client.php';

Assign the URL that has to be shortened to the variable $myUrl. We insert our URL to the URL.

 * The URL that has to be shortened
$myUrl '';
$url '' $myUrl '&format=xml';

Instantiate the Zend_Http_Client object and pass our $url to its constructor. And then send the HTTP request.

= new Zend_Http_Client($url);
$response $client->request();
$xmlString $response->getBody();

We have the XML string returned by assigned to $xmlString. It is time to extract the shortened URL from the response object. We use the PHP SimpleXML extension to extract the information from the XML string.

= new simpleXMLElement($xmlString);
"The shortened url is " $xml->shortened "\n";

I have attached the script so that you can download and play with it.

Taxonomy upgrade extras: 


The is redirecting to page not found. Please correct the link.



Very good post, thanks a lot.

Don't forget about They have an api as well API

Good to know there's a ZF project offering URL shortening service.

I wrote a class that uses the API and contributed it to You can download it from

when i using this it show me zend error which message have

'Message: Read timed out after 10 seconds '

plz solve my problem

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.