Skip to content

Instantly share code, notes, and snippets.

@peterdemartini
Created March 4, 2014 18:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterdemartini/9352894 to your computer and use it in GitHub Desktop.
Save peterdemartini/9352894 to your computer and use it in GitHub Desktop.
SugarCRM Custom JSON API Entry Point
<?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;
}
}
<?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');
}
}
<?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