Guest User

Untitled

a guest
Jun 28th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. [error] => no_method
  2. [error_message] => Could not find a route with 1 elements
  3.  
  4. <?php
  5. $base_url = "https://my.domain/rest/v10";
  6. $username = "admin";
  7. $password = "*********";
  8. function call($url,$oauthtoken='',$type='GET',$arguments=array(),$encodeData=true,$returnHeaders=false){
  9. $type = strtoupper($type);
  10. if ($type == 'GET')
  11. {
  12. $url .= "?" . http_build_query($arguments);
  13. }
  14. $curl_request = curl_init($url);
  15. if ($type == 'POST')
  16. {
  17. curl_setopt($curl_request, CURLOPT_POST, 1);
  18. }
  19. elseif ($type == 'PUT')
  20. {
  21. curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
  22. }
  23. elseif ($type == 'DELETE')
  24. {
  25. curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
  26. }
  27. curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  28. curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
  29. curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
  30. curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
  31. curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
  32. if (!empty($oauthtoken))
  33. {
  34. $token = array("oauth-token: {$oauthtoken}","Content-Type: application/json");
  35. curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token);
  36. }
  37. if (!empty($arguments) && $type !== 'GET')
  38. {
  39. if ($encodeData)
  40. {
  41. //encode the arguments as JSON
  42. $arguments = json_encode($arguments);
  43. }
  44. curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
  45. }
  46. $result = curl_exec($curl_request);
  47. if ($returnHeaders)
  48. {
  49. //set headers from response
  50. list($headers, $content) = explode("rnrn", $result ,2);
  51. foreach (explode("rn",$headers) as $header)
  52. {
  53. header($header);
  54. }
  55. //return the nonheader data
  56. return trim($content);
  57. }
  58. curl_close($curl_request);
  59. //decode the response from JSON
  60. $response = json_decode($result);
  61. return $response;
  62. }
  63.  
  64. //Login - POST /oauth2/token
  65. $url = $base_url . "/oauth2/token";
  66. $oauth2_token_arguments = array(
  67. "grant_type" => "password",
  68. //client id/secret you created in Admin > OAuth Keys
  69. "client_id" => "sugar",
  70. "client_secret" => "",
  71. "username" => $username,
  72. "password" => $password,
  73. "platform" => "base"
  74. );
  75. $oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);
  76. //Create record - POST /<module>/
  77. $url = $base_url . "/Customers"; //works id this is "Accounts"
  78. $record_arguments = array(
  79. "name" => "ACME Inc.",
  80. "description" => "Not for Coyotes"
  81. );
  82. $record_response = call($url, $oauth2_token_response->access_token, 'POST', $record_arguments);
  83. echo "<pre>";
  84. print_r($record_response);
  85. echo "</pre>";
  86.  
  87. <?php
  88.  
  89. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  90.  
  91. require_once("clients/base/api/ModuleApi.php");
  92.  
  93. class CustomOrderApi extends ModuleApi
  94. {
  95. public function registerApiRest()
  96. {
  97. return array(
  98. //GET & POST
  99. 'postOrder' => array(
  100. //request type
  101. 'reqType' => array('POST'),
  102.  
  103. //set authentication
  104. 'noLoginRequired' => false,
  105.  
  106. //endpoint path
  107. 'path' => array('OD_Order_Information'),
  108.  
  109. //endpoint variables
  110. 'pathVars' => array(''),
  111.  
  112. //method to call
  113. 'method' => 'createRecord',
  114.  
  115. //short help string to be displayed in the help documentation
  116. 'shortHelp' => 'An example of a POST endpoint',
  117.  
  118. //long help to be displayed in the help documentation
  119. 'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html',
  120. ),
  121. );
  122. }
  123.  
  124. /**
  125. * Method to be used for my OD_Order_Information/:record endpoint
  126. */
  127. public function createRecord(ServiceBase $api, array $args)
  128. {
  129. $bean = $this->createBean($api, $args);
  130. $data = $this->formatBeanAfterSave($api, $args, $bean);
  131. return 'test';
  132. }
  133.  
  134.  
  135. }
  136.  
  137. ?>
Add Comment
Please, Sign In to add comment