<?php
 
/**
 
 * Communicates with SafeMn service and allows to shorten the long URLs
 
 * 
 
 * @link http://safe.mn/
 
 * @license LGPL
 
 * @author Stanislav Shramko <[email protected]>
 
 */
 
class SafeMn {
 
 
    /**
 
     * SafeNm's API URL
 
     */
 
    const API_URL = 'http://safe.mn/api/';
 
    
 
    /**
 
     * Sends the request and retrieves the answer
 
     * 
 
     * @param string $action the kind of action
 
     * @param string $url the URL to work on
 
     * @throws Exception
 
     * @return string
 
     */
 
    protected static function getResponse($action, $url)
 
    {
 
        if (!$fh = fopen(self::API_URL . "?$action=" . htmlspecialchars($url), "r")) {
 
            throw new Exception("Unable to connect to Safe.mn");
 
        }
 
        $content = '';
 
        while (!feof($fh)) {
 
            $content .= fread($fh, 4096);
 
        }
 
        return trim($content);
 
    }
 
 
    /**
 
     * Shortens the URL
 
     * 
 
     * @param string $url the URL to work on
 
     * @throws Exception
 
     * @return string
 
     */
 
    public static function shortenUrl($url)
 
    {
 
        return self::getResponse('url', $url);
 
    }
 
    
 
    /**
 
     * Tracks the URL
 
     * 
 
     * @param string $url the URL to work on
 
     * @throws Exception
 
     * @return string
 
     */
 
    public static function trackUrl($url)
 
    {
 
        return self::getResponse('short_url', $url);
 
    }
 
    
 
}
 
 
?>
 
 |