Created
March 4, 2014 18:44
-
-
Save peterdemartini/9352894 to your computer and use it in GitHub Desktop.
SugarCRM Custom JSON API Entry Point
This file contains hidden or 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 CustomAPI { | |
var $inbound = false; | |
public function __construct(){ | |
$this->inbound = $this->parse_inbound_json(); | |
} | |
public function get_inbound(){ return $this->inbound; } | |
/** | |
* @success = boolean | |
* @response = text | |
* @data = array | |
*/ | |
public function handle_response($success, $response, $data = array()){ | |
die( | |
json_encode( | |
array( | |
'success' => $success, | |
'response' => $response, | |
'data' => $data | |
) | |
) | |
); | |
} | |
/** | |
* Parse Inbound JSON | |
* return = (array) | |
*/ | |
private function parse_inbound_json(){ | |
$input_json = file_get_contents('php://input'); | |
$decode = json_decode($input_json, true); | |
if($decode == NULL){ | |
handle_response(false, 'Invalid JSON'); | |
} | |
return $decode; | |
} | |
} |
This file contains hidden or 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 | |
require_once('custom/modules/Tasks/CustomAPI.php'); | |
class TasksAPI extends CustomAPI { | |
public function __construct(){ | |
parent::__construct(); | |
} | |
public function call_method(){ | |
switch ($this->inbound['method']) { | |
case 'test': | |
$this->test(); | |
return; | |
default: | |
//No method found | |
$this->handle_response(false, 'Invalid Request'); | |
break; | |
} | |
} | |
public function test(){ | |
$this->handle_response(true, 'Successful Test'); | |
} | |
} |
This file contains hidden or 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 | |
/******** | |
* Created By Peter DeMartini (SierraCRM) | |
* 03-4-2014 | |
*********/ | |
//Requires | |
require_once('data/SugarBean.php'); | |
require_once('custom/modules/Tasks/TaskAPI.php'); | |
global $app_strings, | |
$app_list_strings, | |
$sugar_config, | |
$timedate, | |
$current_user, | |
$db; | |
$api = new TasksAPI(); | |
$api->call_method(); | |
// Default Response | |
$api->handle_response(false, 'Invalid Response'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment