Location: PHPKode > scripts > URL Shorten using short.ie > url-shorten-using-short-ie/ShortIE.class.php
<?php

/**
 * ShortIe URL shortening class
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @copyright  Copyright (c) 2009 Sudheera Satyanarayana (http://techchorus.net and http://sudheer.net)
 * @license    New BSD License
 */


/**
 * Class to shorten a given URL using http://short.ie  API 
 */
class ShortIe 
{
    /**
     * @property (string) is the URL to be shortened
     */
    protected $_urlToBeShortened;

    /**
     * @property (string) is the URL shortened by short.ie API call
     */
    protected $_shortenedUrl;

    /**
     * @param optional url (string) URL to be shortened
     */
    public function __construct($url = null)
    {
        if ( !($url === null) ) {
            $this->setUrlToBeShortened($url);
        }
    }

    /**
     * @param url (string) URL to be shortened
     */
    public function setUrlToBeShortened($url)
    {
        $this->_urlToBeShortened = $url;
    }

    protected function _shorten()
    {
        $ch = curl_init(); 

        $shortIePartUrl = "http://short.ie/api?url=";
        $shortIePartUrl .= urlencode($this->_urlToBeShortened);
        $shortIePartUrl .= '&format=xml';

        curl_setopt($ch, CURLOPT_URL, $shortIePartUrl);
    
        /**
         * return the transfer as a string
         */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        /**
         * $output contains the output string
         */
        $output = curl_exec($ch);
        
        $xml = new simpleXMLElement($output);
        
        $this->_shortenedUrl = $xml->shortened;

        curl_close($ch);       
       
    }
  
    /**
     * @return the shortened URL after calling the short.ie API
     */ 
    public function getShortenedUrl()
    {
        $this->_shorten();
        return $this->_shortenedUrl;
    }


}
Return current item: URL Shorten using short.ie