<?php
/**
* A SimpleXML and cURL Implementation of the HostInquest API
* @package inquest
* @author J Watkins <krakjoe@krakjoe.info>
* @copyright 2007 Hostinquest.com
*/
class inquest
{
/**
* Email address you registered on hostinquest.com
*
* @var string
*/
var $email ;
/**
* Your activated API key
*
* @var string
*/
var $key ;
/**
* Use SSL when connecting with Hostinquest.com
*
* @var bool
*/
var $ssl ;
/**
* cURL Resource
*
* @var resource
*/
var $curl ;
/**
* Object ready to operate
*
* @var bool
*/
var $loaded ;
/**
* Cached XML
*
* @var string
*/
var $cache ;
/**
* Parsed XML object
*
* @var object
*/
var $xml ;
/**
* Errors array
*
* @var array
*/
var $errors ;
/**
* Construct Object
*
* @param string $email
* @param string $key
* @param bool $ssl
* @return inquest
*
* <code><?php $inquest = new inquest( 'email', 'key', true ); ?></code>
*/
function inquest( $email, $key, $ssl = false )
{
$this->email = $email ;
$this->key = $key ;
$this->ssl = $ssl;
$this->curl = $curl ;
$this->loaded = false ;
$this->cache = null ;
$this->xml = null ;
$this->errors = array( );
if( function_exists( 'curl_init' ) )
{
if( ( $this->curl = curl_init( sprintf( 'http%s://hostinquest.com/api', $this->ssl ? 's' : '' ) ) ) )
{
if( $this->ssl )
{
if( !curl_setopt( $this->curl, CURLOPT_SSL_VERIFYHOST, false ) or
!curl_setopt( $this->curl, CURLOPT_SSL_VERIFYPEER, false ) )
{
$this->error( __METHOD__, "cannot setup SSL", $this->cerror( ) );
}
}
if( !curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, true ) or
!curl_setopt( $this->curl, CURLOPT_HEADER, 0 ) or
!curl_setopt( $this->curl, CURLOPT_POST, true ) )
{
$this->error( __METHOD__, "cannot setup curl transfer", $this->cerror( ) );
}
$this->loaded( );
}
else $this->error( __METHOD__, "cannot setup curl", $this->cerror( ) );
}
else $this->error( __METHOD__, "requires curl_init" );
}
/**
* Get last cURL error
*
* @return string
*
* <code><?php echo $inquest->cerror( ); ?></code>
*/
function cerror( )
{
if( ( $errno = curl_errno( $this->curl ) ) and
( $errstr = curl_error( $this->curl ) ) )
{
return sprintf( '%d : %s', $errno, $errstr );
}
}
/**
* Add an error to the errors array
*
* @param string $method
* @param string $message
* @param string $extra
*
* <code><?php $inquest->error( __METHOD__, 'an error occured' ); ?></code>
*/
function error( $method, $message, $extra = null )
{
$this->errors[ ] = sprintf( '%s %s%s', $method, $message, $extra ? sprintf( ' %s', $extra ) : '' ) ;
}
/**
* Lazy, return errors array
*
* @return array
*
* <code><?php $errors = $inquest->errors( ); ?></code>
*/
function errors( )
{
return $this->errors ;
}
/**
* Determine if object is ready for operation
*
* @return bool
*
* <code><?php if( $inquest->loaded( ) ) echo 'Loaded'; ?></code>
*/
function loaded( )
{
$this->loaded = count( $this->errors( ) ) == 0 ? true : false ;
return $this->loaded ;
}
/**
* Returns raw xml from an API request
*
* @param string $function
* @param array $params
* @return string
*
* <code><?php echo $inquest->call( 'enter', array( 'service' => 'myhost.com', 'domain' => 'thehosted.com', 'umail' => 'them@thehosted.com' ) ); ?></code>
*/
function call( $function, $params = array( ) )
{
$request[ ] = sprintf('email=%s', urlencode( $this->email ) ) ;
$request[ ] = sprintf('key=%s', $this->key );
if( count( $params ) > 0 )
{
foreach( $params as $key => $value )
{
$request[ ] = sprintf('%s=%s', $key, urlencode( $value ) );
}
}
if( !curl_setopt( $this->curl, CURLOPT_POSTFIELDS, implode( '&', $request ) ) )
{
$this->error( __METHOD__, 'cannot setup post data', $this->cerror( ) );
}
if( !( $result = curl_exec( $this->curl ) ) )
{
$this->error( __METHOD__, 'cannot send HTTP request', $this->cerror( ) );
}
else return $result ;
}
/**
* Parse XML from an API call
*
* @param string $function
* @param array $params
* @return string
*
* <code><?php $xml = $inquest->xml( 'enter', array( 'service' => 'myhost.com', 'domain' => 'thehosted.com', 'umail' => 'them@thehosted.com' ) ); ?></code>
*/
function xml( $function, $params = array( ) )
{
if( ( $this->cache = $this->call( $function, $params ) ) )
{
$this->xml = new SimpleXMLElement( $this->cache );
if( !$this->xml->error and !$this->xml->errors )
{
return $this->xml->inquest ;
}
elseif( $this->xml->error )
{
$this->error( __METHOD__, sprintf( "recieved %s from HostInquest API", $this->xml->error ) );
}
elseif( $this->xml->errors )
{
foreach( $this->xml->errors as $error )
{
$this->error( __METHOD__, sprintf( "recieved %s from HostInquest API", $this->xml->error ) );
}
}
}
}
/**
* Return cached raw xml
*
* @return string
*
* <code><?php echo $inquest->cache( ); ?></code>
*/
function cache( )
{
return $this->cache ;
}
}
$inquest = new inquest( 'registered@email.com', 'APIKEY', true );
if( $inquest->loaded( ) )
{
$inquest->xml( 'check' ); // this will fail, the function doesn't exist, just want some data
echo "<pre>";
print_r( $inquest ); // heres the data .....
}
?>