<?php

class urlBorg {
/*
	Name: urlborg.php
	Version: 0.1
	Date: 13-Feb-2008
	Creator: Panayotis Vryonis <vrypan@gmail.com>
	URL: http://www.urlb.org/developers/

	Description: PHP Class that implements the urlB.org API.

	example usage:
		require_once 'urlborg.php' ;
		$ub = new urlBorg('use your API key here') ;
	
		$info = $ub->get_info('lgfy') ;
		if ($ub->get_error()) { 
			echo '<br/>ERROR! Code:' . $ub->get_error('n') . ' desc:' . $ub->get_error('s') ; 
		} else {
			print_r($info) ;
		}

		$short = $ub->create_new('http://vrypan.net/') ;
		if ($ub->get_error()) { 
			echo '<br/>ERROR! Code:' . $ub->get_error('n') . ' desc:' . $ub->get_error('s') ; 
		} else {
			print_r($short) ;
		}
*/

	private $APIKEY ;
	private $ERROR_CODE ;
	function __construct($apikey) {
		$this->APIKEY = $apikey ;
		$this->ERROR_CODE = 0 ;
	}

	function get_info($urlkey) {
		$this->reset_errors() ;
		$xml_h = fopen('http://www.urlb.org/api/?action=info&apikey='. $this->APIKEY . '&urlkey='. $urlkey ,'r') ;
		while (!feof($xml_h) ) { $xml .= fgets($xml_h,1024) ; }
		fclose($xml_h) ;
		$obj = new SimpleXMLElement($xml);

		if (isset($obj->error->code)) {
			$this->ERROR_CODE = $obj->error->code ;
			$this->ERROR_MSG = $obj->error->message ;
			return false ;
		}
		return $obj ;
	}

	function create_new($url) {
		$this->reset_errors() ;
		$xml_h = fopen('http://www.urlb.org/api/?action=create&apikey='. $this->APIKEY . '&url='. urlencode($url),'r') ;
		while (!feof($xml_h) ) { $xml .= fgets($xml_h,1024) ; }
		fclose($xml_h) ;
		$obj = new SimpleXMLElement($xml);

		if (isset($obj->error->code)) {
			$this->ERROR_CODE = $obj->error->code ;
			$this->ERROR_MSG = $obj->error->message ;
			return false ;
		}

		return $obj ;
	}

	function reset_errors() {
		$this->ERROR_CODE=0 ;
		$this->ERROR_MSG = '' ;
	}
	function get_error($format='n') {
		switch($format) {
			case 's':
			case 'str': $ret = $this->ERROR_MSG ; break ;

			case 'n' : 
			case 'num' : 
			default: $ret = $this->ERROR_CODE ; break ;
		}
		return $ret ;
	}

}
?>

