For years, we developers tend to keep functions that we find useful into our projects, after certain time they become obsolete or you move one to higher level ways of programming. Some of us, can’t get rid of the code that was once collected-created and I thought let’s see if I post one of my silly old classes what can we get out of it? Let’s make it open source hahahaha.

I propose you the following: Here is my small old good set of Uri PHP functions encapsulated into one class, I know is not a great deal but, let’s see what can we all do with it. For every programmer that posts a function, I will write his reference into the class and post his/her site link in this post.

There are certain rules though:

  1. Functions cannot be greater than 20 lines of code
  2. Functions cannot depend of far too many external components (maximum one)
  3. If functions depend of an external component/class, a URL indicating its reference must be provided
  4. Functions are not to be repetitive, if there is one better written that the ones exposes we will replace it

The Uri Class

Here is my class:

<?php
/**
 * @author Antonio Ramirez <contactme@atmyblog.com>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link http://www.ramirezcobos.com/
 *
 *
 */
class Uri
{
	/**
	 * Encodes the parameter to hide
	 * its contents to user
	 *
	 * @param string $val
	 */
	function uriEncode($val)
	{
		return rawurlencode(base64_encode($val));
	}
	/**
	 * Decodes a parameter string that
	 * was previously encoded with uriEncode
	 * function
	 *
	 * @param string $val
	 */
	function uriDecode($val){
		return base64_decode(rawurldecode($val));
	}
	/**
	 * Translates a given url to
	 * tinyurl version -curl required
	 *
	 * @param string $url
	 * @access public
	 * @return string $data
	 */
	function getTinyUrl($url){
		$ch = curl_init();
		$timeout = 5;
		curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
	/**
	 * Translates a given url to
	 * ToLyUrl version -curl required
	 *
	 * @param string $url
	 * @access public
	 * @return string $data
	 */
	function getToLyUrl($url){
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, 'http://to.ly/api.php?longurl='.urlencode($url));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_HEADER, 0);

		$data = curl_exec ($ch);
		curl_close ($ch);

		return $data;
	}
	/**
     * Gets Request param
     *
     * @param string $paramName
     * @param string $slash
     * @access public
     * @return string $param or boolean $false
     */
	function getParam($paramName, $slash=false)
	{
		return (isset($_REQUEST[$paramName])?($slash?addslashes($_REQUEST[$paramName]):$_REQUEST[$paramName]):false);
	}
	/**
     * Serializes Request params
     *
     * @param string $notThisOne
     * @access public
     * @return string $query_string
     */
	function getParams($notThisOne=false)
  	{
		global $REQUEST_METHOD, $HTTP_GET_VARS, $HTTP_POST_VARS;
		$query_string='';
		if (strpos($_SERVER['REQUEST_URI'],"?"))
		{
			list($fullfile, $voided) = explode("?", $_SERVER['REQUEST_URI']);
			$cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $HTTP_GET_VARS : $HTTP_POST_VARS;
			reset ($cgi);
			while (list($key, $value) = each($cgi)) {
			  if ($key != $notThisOne)
				$query_string .= "&" . $key . "=" . $value;
			}
		}
		return $query_string;
  	}
	/**
     * Makes a Soap Call
     *
     * @param string $uri
     * @param string $command
     * @param string $xml
     * @access public
     * @return string $response
     *
     * Requires Soap
     */
	function makeSoapCall($uri,$command,$xml)
	{

		try{

			ini_set('default_socket_timeout',10); // ten seconds, that is all we wait

			$client = new SoapClient($uri,array("connection_timeout"=>10));

			$response =  $client->__soapCall($command,array(new SoapParam($xml,'Peticion')));
		}
		catch(Exception $e){

			$response = false;
		}
		return $response;
	}
	/**
	 * Parses a given $url for validation
	 *
	 * @param string $url
	 * @access public
	 * @return $parsed_url
	 */
	function getParsedUrl($url)
	{
		$parsed_url = parse_url($url);

		if (!isset($parsed_url['scheme']) or $parsed_url['scheme'] != 'http'){
			echo 'Unsupported URL sheme given, please just use "HTTP".';
			exit();
		}
		if (!isset($parsed_url['host']) or $parsed_url['host'] == ''){
			echo 'Invalid URL given!';
			exit();
		}

		$host = $parsed_url['host'];
		$host .= (isset($parsed_url['port']) and  !empty($parsed_url['port'])) ? ':'.$parsed_url['port'] : '';
		$path = (isset($parsed_url['path']) and  !empty($parsed_url['path'])) ? $parsed_url['path'] : '/';
		$path .= (isset($parsed_url['query']) and  !empty($parsed_url['query'])) ? '?'.$parsed_url['query'] : '';

		return 'http://' . $host . $path;
	}
	/**
     * Requests a Url and returns its contents
     *
     * @param string $url
     * @access public
     * @return string $query_string
     * @uses Snoopy class http://sourceforge.net/projects/snoopy/
     */
	function getUrlContent($url)
	{
		App::import('component','snoopy');

		$url = Uri::getParsedUrl($url);

		$snoopy = new Snoopy();
		$snoopy->agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; es-ES; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12';
		$snoopy->referer = '';

		$results = false;

		if($snoopy->fetch($url)){

			$results = $snoopy->results;
			// empty buffer:
			$snoopy->results = '';
		}

		return $results;
	}
}
?>