PHP Script To Shorten URLs Using short.ie

AttachmentSize
short.ie-url-shortening.php_.tar_.gz1.34 KB

We all know about the URL shortening service offered by many websites. Most notable among them is tinyurl.com. 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 short.ie 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.


<?php
/**
 * @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 short.ie/api URL.

<?php
/**
 * The URL that has to be shortened
 */
$myUrl 'http://example.com/very/long/url/here';
$url 'http://short.ie/api?url=' $myUrl '&format=xml';
?>

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

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

We have the XML string returned by short.ie 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.

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

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

Taxonomy upgrade extras: 

Comments

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

Corrected.

Thanks.

Very good post, thanks a lot.

Hi,
Don't forget about zfurl.us They have an api as well Zfurl.us API

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

I wrote a class that uses the short.ie API and contributed it to PHPClasses.org. You can download it from http://www.phpclasses.org/browse/package/5538.html

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.