PHP Script To Shorten URLs Using short.ie

Loading

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.

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

Sudheer is an entrepreneur and software developer. Get more from Sudheer on Twitter.


The tinyurl.com is

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

Fixed

Corrected.

Thanks.

Very good post, thanks a lot.

Very good post, thanks a lot.

Don't forget about zfurl.us

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

Good to know there's a ZF

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

Implementation without ZF

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

shorten url error

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

'Message: Read timed out after 10 seconds '

plz solve my problem

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
By submitting this form, you accept the Mollom privacy policy.