CakePHP whmcs module – interacting with whmcs api via Cake

by c

<?php

App::import('Core', array('HttpSocket'));

class WhmcsComponent extends Object {
/**
* WHMCS component
*
* Rudimentary wrapper for curl-based API connection to WHMCS.
*
* PHP version 5
*
* Licensed under The MIT License
*
* @lastmodified    Date: March 24, 2009
* @license        http://www.opensource.org/licenses/mit-license.php The MIT License
*/

/**
* URL of WHMCS api.
*
* @var string
* @access public
*/
var $api_url = 'http://yoursite.com/whmcs/includes/api.php';

/**
* Admin username for WHMCS access
*
* @var string
* @access public
*/
var $api_username = 'adminuser';

/**
* Admin password for WHMCS access
*
* @var string
* @access public
*/
var $api_password = 'adminpassword';

/**
* Holds last WHMCS resultset
*
* @var array
* @access public
*/
var $last_result = Array();
function __construct() {
$this->Http = new HttpSocket();
}

/**
* Raw access to WHMCS API. Throws warning and returns false on any error, returns array-based resultset otherwise.
*
*    $this->Whmcs->api('addclient', Array(
*        "firstname" => "test",
*        "lastname" => "test",
*        "companyname" => "test",
*        "email" => "test@test.com",
*        "address1" => "test",
*        "address2" => "test",
*        "city" => "test",
*        "state" => "test",
*        "postcode" => "1231231",
*        "country" => "test",
*        "phonenumber" => "5551212",
*        "password2" => "test",
*    ));
*
* @param string $action WHMCS action to be performed. ("addclient" for example)
* @param array $data Array of data to be sent to WHMCS.
* @access public
*/

function api($action, $data)
{

// Make sure WHMCS username and password have been set
if ($this->api_username == 'adminuser' || $this->api_password == 'adminpassword')
{
trigger_error(__('Can\'t connect to WHMCS without api username and password.', true), E_USER_WARNING);
return false;
}
// Data to be sent to WHMCS.
$_pass_data = array_merge(
Array(
"username" => $this->api_username,
"password" => md5($this->api_password),
"action" => $action,
),
$data
);

// Connect to WHMCS
$_result = $this->Http->post($this->api_url, http_build_query($_pass_data));

$code = (int)$this->Http->response['status']['code'];

if ($_result === null || $_result === false || preg_match('/^(4|5)/', $code))
{
trigger_error(__(
'Error from HttpSocket: HTTP code '.$code.
($this->Http->lastError()?', HttpSocket error code: '.$this->Http->lastError():'')
, true), E_USER_WARNING);
return false;
}
// Parse returned data from WHMCS
$_return = Array();

foreach (explode(';', $_result) as $x)
{
parse_str($x, $a);
$_return = array_merge($_return, $a);
}
$this->last_result = $_return;
// Check for invalid dataset
if (count($_return) == 0)
{
trigger_error(__('WHMCS returned empty result set.', true), E_USER_WARNING);
return false;
}

// Check for WHMCS result error
if ($_return['result'] === 'error' && preg_match('/^Authentication/', $_return['message']))
{
trigger_error(__('WHMCS returned error: '.$_return['message'], true), E_USER_WARNING);
return false;
}

// Return WHMCS data
return (array)$_return;
}

}
?>