Created
June 3, 2015 15:20
-
-
Save Blindmikey/4adf07f7d0736feed172 to your computer and use it in GitHub Desktop.
PHP SugarCRM Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SugarAPI | |
{ | |
/** | |
* Default Class Settings | |
* @var array | |
*/ | |
protected $s; | |
/** | |
* Class Error Indicator | |
* @var boolean | |
*/ | |
public $foundError; | |
function __construct($user_settings) | |
{ | |
$this->s = wp_parse_args( $user_settings, array( | |
'user' => '', | |
'pass' => '', | |
'key' => '', | |
'secret' => '', | |
'sugarUrl' => '', | |
'method' => 'GET', | |
'encodeData' => true, | |
'returnHeaders' => false, | |
'tokens' => array() | |
)); | |
$this->foundError = false; | |
if ( | |
!$this->s['user'] || | |
!$this->s['pass'] || | |
!$this->s['key'] || | |
!$this->s['secret'] || | |
!$this->s['sugarUrl'] | |
) { | |
trigger_error('SugarAPI -> Failed due to invalid settings.'); | |
return false; | |
} | |
$response = $this->handshake(array( | |
'grant_type' => 'password', | |
'client_id' => $this->s['key'], | |
'client_secret' => $this->s['secret'], | |
'username' => $this->s['user'], | |
'password' => $this->s['pass'], | |
'platform' => 'base' | |
)); | |
if ( false === $response ) { | |
$this->foundError = true; | |
} | |
} | |
/** | |
* Obtains short term OAuth token for future interactions | |
* | |
* @access private | |
* @param array $arguments array of arguments | |
* @return void adds tokens to $this->s['tokens'] | |
*/ | |
private function handshake($arguments) | |
{ | |
$url = $this->s['sugarUrl'] . '/oauth2/token'; | |
$response = $this->sugarCURL($url, 'POST', $arguments); | |
if ( ! is_object($response) || ! isset($response->access_token) ) { | |
trigger_error(print_r(array('SugarAPI -> Handshake Failed. Response: ', $response), true)); | |
return false; | |
} | |
$this->s['tokens']['access'] = array( | |
'token' => $response->access_token, | |
'type' => $response->token_type, | |
'life' => $response->expires_in, | |
'scope' => $response->scope | |
); | |
$this->s['tokens']['refresh'] = array( | |
'token' => $response->refresh_token, | |
'life' => $response->refresh_expires_in | |
); | |
$this->s['tokens']['download'] = array( | |
'token' => $response->download_token, | |
'life' => $response->expires_in, | |
); | |
return true; | |
} | |
/** | |
* Retrieves one or all sugar records in a module | |
* | |
* @access private | |
* @param array $args array of arguments. required: module | |
* @return object the record or records | |
*/ | |
private function sugarGET( $args ) | |
{ | |
$s = wp_parse_args( $args, array( | |
'module' => '', | |
'record' => '', | |
'args' => array() | |
)); | |
if ( ! $s['module'] ) { | |
trigger_error('SugarAPI -> Failed GET. No Module Specified.'); | |
return false; | |
} | |
$url = $this->s['sugarUrl'] . '/' . $s['module']; | |
if ( $s['record'] ) { | |
$url .= '/' . $s['record']; | |
} | |
$response = $this->sugarCURL($url, 'GET', $s['args']); | |
if ( ! is_object($response) || (count(get_object_vars($response)) < 3) ) { | |
trigger_error(print_r(array('SugarAPI -> Failed GET. Response: ', $response), true)); | |
return false; | |
} | |
return $response; | |
} | |
/** | |
* Deletes a sugar record | |
* | |
* @access private | |
* @param array $args array of arguments. required: module, record | |
* @return string Record ID deleted, false on failure | |
*/ | |
private function sugarDELETE( $args ) | |
{ | |
$s = wp_parse_args( $args, array( | |
'module' => '', | |
'record' => '', | |
'args' => array() | |
)); | |
if ( ! $s['module'] ) { | |
trigger_error('SugarAPI -> Failed DELETE. No Module Specified.'); | |
return false; | |
} | |
if ( ! $s['record'] ) { | |
trigger_error('SugarAPI -> Failed DELETE. No Record Specified.'); | |
return false; | |
} | |
$url = $this->s['sugarUrl'] . '/' . $s['module'] . '/' . $s['record']; | |
$response = $this->sugarCURL($url, 'DELETE', $s['args']); | |
if ( ! is_object($response) || ! isset($response->id) ) { | |
trigger_error(print_r(array('SugarAPI -> Failed DELETE. Response: ', $response), true)); | |
return false; | |
} | |
return $response->id; | |
} | |
/** | |
* Adds a new sugar record | |
* | |
* @access private | |
* @param array $args array of arguments. required: module, args | |
* @return mixed record ID on success, false on failure | |
*/ | |
private function sugarPOST( $args ) | |
{ | |
$s = wp_parse_args( $args, array( | |
'module' => '', | |
'args' => array() | |
)); | |
if ( ! $s['module'] ) { | |
trigger_error('SugarAPI -> Failed POST. No Module Specified.'); | |
return false; | |
} | |
if ( ! $s['args'] ) { | |
trigger_error('SugarAPI -> Failed POST. No Parameters Specified.'); | |
return false; | |
} | |
$url = $this->s['sugarUrl'] . '/' . $s['module']; | |
$response = $this->sugarCURL($url, 'POST', $s['args']); | |
if ( ! is_object($response) || ! isset($response->id) ) { | |
trigger_error(print_r(array('SugarAPI -> Failed POST. Response: ', $response), true)); | |
return false; | |
} | |
return $response->id; | |
} | |
/** | |
* Updates existing sugar record | |
* | |
* @access private | |
* @param array $args array of arguments. required: module, record, args | |
* @return string record ID on success, false on failure | |
*/ | |
private function sugarPUT( $args ) | |
{ | |
$s = wp_parse_args( $args, array( | |
'module' => '', | |
'record' => '', | |
'args' => array() | |
)); | |
if ( ! $s['module'] ) { | |
trigger_error('SugarAPI -> Failed PUT. No Module Specified.'); | |
return false; | |
} | |
if ( ! $s['record'] ) { | |
trigger_error('SugarAPI -> Failed PUT. No Record Specified.'); | |
return false; | |
} | |
if ( ! $s['args'] ) { | |
trigger_error('SugarAPI -> Failed PUT. No Parameters Specified.'); | |
return false; | |
} | |
$url = $this->s['sugarUrl'] . '/' . $s['module'] . '/' . $s['record']; | |
$response = $this->sugarCURL($url, 'PUT', $s['args']); | |
if ( ! is_object($response) || ! isset($response->id) ) { | |
trigger_error(print_r(array('SugarAPI -> Failed PUT. Response: ', $response), true)); | |
return false; | |
} | |
return $response->id; | |
} | |
/** | |
* Handles CURL requests to SugarCRM | |
* | |
* @access private | |
* @param string $url sugar endpoint | |
* @param string $method GET, POST, PUT, DELETE | |
* @param array $arguments array of action parameters | |
* @return array response from SugarCRM's REST API | |
*/ | |
private function sugarCURL( $url, $method, $arguments ) | |
{ | |
$method = strtoupper($method); | |
if ( 'GET' == $method ) { | |
$url .= "?" . http_build_query($arguments); | |
} | |
$curl_request = curl_init($url); | |
if ( 'POST' == $method ) { | |
curl_setopt($curl_request, CURLOPT_POST, 1); | |
} | |
else if ( 'PUT' == $method ) { | |
curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT"); | |
} | |
else if ( 'DELETE' == $method ) { | |
curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
} | |
else if ( 'GET' != $method ) { | |
trigger_error('SugarAPI -> Failed CURL. Invalid Method.'); | |
return false; | |
} | |
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | |
curl_setopt($curl_request, CURLOPT_HEADER, $this->s['returnHeaders']); | |
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0); | |
if ( isset($this->s['tokens']['access']) ) { | |
curl_setopt($curl_request, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'OAuth-Token: '.$this->s['tokens']['access']['token'] )); | |
} | |
if ( !empty($arguments) && 'GET' !== $method ) | |
{ | |
if ($this->s['encodeData']) | |
{ | |
$arguments = json_encode($arguments); | |
} | |
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments); | |
} | |
$result = curl_exec($curl_request); | |
if ($this->s['returnHeaders']) | |
{ | |
//set headers from response | |
list($headers, $content) = explode("\r\n\r\n", $result ,2); | |
foreach (explode("\r\n",$headers) as $header) | |
{ | |
header($header); | |
} | |
//return the nonheader data | |
return trim($content); | |
} | |
curl_close($curl_request); | |
//decode the response from JSON | |
$response = json_decode($result); | |
return $response; | |
} | |
/** | |
* Add a Lead to SugarCRM | |
* | |
* @access public | |
* @param array $args array of arguments. required: args | |
* @return mixed @see output of $this->sugarPOST() | |
*/ | |
public function addLead( $args ) | |
{ | |
$newLead = $this->sugarPOST( array( | |
'module' => 'Leads', | |
'args' => $args | |
)); | |
return $newLead; | |
} | |
/** | |
* Update a Specified Lead in SugarCRM | |
* | |
* @access public | |
* @param string $leadID the lead ID | |
* @param array $args array of arguments. required: args | |
* @return mixed @see output of $this->sugarPUT() | |
*/ | |
public function updateLead( $leadID, $args ) | |
{ | |
$update = $this->sugarPUT( array( | |
'module' => 'Leads', | |
'record' => $leadID, | |
'args' => $args | |
)); | |
return $update; | |
} | |
/** | |
* Delete a Specified Lead in SugarCRM | |
* | |
* @access public | |
* @param string $leadID the lead ID | |
* @return mixed @see output of $this->sugarDELETE() | |
*/ | |
public function deleteLead( $leadID ) | |
{ | |
$deleted = $this->sugarDELETE( array( | |
'module' => 'Leads', | |
'record' => $leadID, | |
)); | |
return $deleted; | |
} | |
/** | |
* Get Leads in SugarCRM | |
* | |
* @access public | |
* @return mixed @see output of $this->sugarGET() | |
*/ | |
public function getLeads() | |
{ | |
$leads = $this->sugarGET( array( | |
'module' => 'Leads' | |
)); | |
return $leads; | |
} | |
/** | |
* Gets a Specified Lead in SugarCRM | |
* | |
* @access public | |
* @param string $leadID the lead ID | |
* @return mixed @see output of $this->sugarGET() | |
*/ | |
public function getLead( $leadID ) | |
{ | |
$lead = $this->sugarGET( array( | |
'module' => 'Leads', | |
'record' => $leadID | |
)); | |
return $lead; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Relies on Wordpress' wp_parse_args() which can be copied and used without Wordpress if needed.