Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [error] => no_method
- [error_message] => Could not find a route with 1 elements
- <?php
- $base_url = "https://my.domain/rest/v10";
- $username = "admin";
- $password = "*********";
- function call($url,$oauthtoken='',$type='GET',$arguments=array(),$encodeData=true,$returnHeaders=false){
- $type = strtoupper($type);
- if ($type == 'GET')
- {
- $url .= "?" . http_build_query($arguments);
- }
- $curl_request = curl_init($url);
- if ($type == 'POST')
- {
- curl_setopt($curl_request, CURLOPT_POST, 1);
- }
- elseif ($type == 'PUT')
- {
- curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
- }
- elseif ($type == 'DELETE')
- {
- curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
- }
- curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
- curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
- if (!empty($oauthtoken))
- {
- $token = array("oauth-token: {$oauthtoken}","Content-Type: application/json");
- curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token);
- }
- if (!empty($arguments) && $type !== 'GET')
- {
- if ($encodeData)
- {
- //encode the arguments as JSON
- $arguments = json_encode($arguments);
- }
- curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
- }
- $result = curl_exec($curl_request);
- if ($returnHeaders)
- {
- //set headers from response
- list($headers, $content) = explode("rnrn", $result ,2);
- foreach (explode("rn",$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;
- }
- //Login - POST /oauth2/token
- $url = $base_url . "/oauth2/token";
- $oauth2_token_arguments = array(
- "grant_type" => "password",
- //client id/secret you created in Admin > OAuth Keys
- "client_id" => "sugar",
- "client_secret" => "",
- "username" => $username,
- "password" => $password,
- "platform" => "base"
- );
- $oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);
- //Create record - POST /<module>/
- $url = $base_url . "/Customers"; //works id this is "Accounts"
- $record_arguments = array(
- "name" => "ACME Inc.",
- "description" => "Not for Coyotes"
- );
- $record_response = call($url, $oauth2_token_response->access_token, 'POST', $record_arguments);
- echo "<pre>";
- print_r($record_response);
- echo "</pre>";
- <?php
- if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
- require_once("clients/base/api/ModuleApi.php");
- class CustomOrderApi extends ModuleApi
- {
- public function registerApiRest()
- {
- return array(
- //GET & POST
- 'postOrder' => array(
- //request type
- 'reqType' => array('POST'),
- //set authentication
- 'noLoginRequired' => false,
- //endpoint path
- 'path' => array('OD_Order_Information'),
- //endpoint variables
- 'pathVars' => array(''),
- //method to call
- 'method' => 'createRecord',
- //short help string to be displayed in the help documentation
- 'shortHelp' => 'An example of a POST endpoint',
- //long help to be displayed in the help documentation
- 'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html',
- ),
- );
- }
- /**
- * Method to be used for my OD_Order_Information/:record endpoint
- */
- public function createRecord(ServiceBase $api, array $args)
- {
- $bean = $this->createBean($api, $args);
- $data = $this->formatBeanAfterSave($api, $args, $bean);
- return 'test';
- }
- }
- ?>
Add Comment
Please, Sign In to add comment