PHP Classes

File: SafeMn.php

Recommend this page to a friend!
  Classes of Stanislav Shramko   SafeMn   SafeMn.php   Download  
File: SafeMn.php
Role: Class source
Content type: text/plain
Description: The code
Class: SafeMn
Create short URLs using safe.mn API
Author: By
Last change: Typo
Date: 15 years ago
Size: 1,260 bytes
 

Contents

Class file image Download
<?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);
    }
   
}

?>