如何使用 REST API 访问与 crm 套件中模块相关的 table 数据
How to access data of a table related to a module in suite crm using REST API
$entryArgs = array(
'session' => $sessId,
'module_name' => 'SecurityGroups',
'query' => "securitygroups_users.securitygroup_id = 'id`enter code
here`'",
'order_by' => '',
'offset' => 0,
'select_fields' => array('id','name'),
'max_results' => 10,
'deleted' => 0 );
$module_result = restRequest('get_entry_list',$entryArgs);
echo "<pre>";
print_r($module_result);
echo "</pre>";
我想通过 REST API 从套件 CRM securitygroup_users table 中获取数据。但我什么也没得到。任何帮助将不胜感激。
我想写一个自定义的 Rest API 你可以按照这个 link Custom Rest API
扩展 Web 服务
概述
本指南将演示如何将您自己的自定义方法添加到 REST 和 SOAP API 或扩展现有方法。
扩展 API
下面的例子将演示如何扩展v4_1API。
定义入口点位置
您可以在此处定义包含新 REST 和 SOAP 入口点的目录。我们推荐的路径格式如下:
./custom/service/{version}_custom/
入口点的实际位置并不重要,但是,使用这样的路径将允许您按如下方式调用入口点:
http://{sugar_url}/custom/service/{version}_custom/rest.php
http://{sugar_url}/custom/service/{version}_custom/soap.php
定义 SugarWebServiceImpl Class
下一步是定义一个新的“SugarWebServiceImpl
”class。由于我们正在使用 v4_1,因此我们需要扩展文件 'service/v4_1/SugarWebServiceImplv4_1.php'
并添加我们的新方法。为此,我们将创建文件:
./custom/service/v4_1_custom/SugarWebServiceImplv4_1_custom.php
<?php
if(!defined('sugarEntry'))define('sugarEntry', true);
require_once('service/v4_1/SugarWebServiceImplv4_1.php');
class SugarWebServiceImplv4_1_custom extends SugarWebServiceImplv4_1
{
/*
* Returns the session id if authenticated
*
* @param string $session
* @return string $session - false if invalid.
*
*/
function example_method($session)
{
$GLOBALS['log']->info('Begin: SugarWebServiceImplv4_1_custom->example_method');
$error = new SoapError();
//authenticate
if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error))
{
$GLOBALS['log']->info('End: SugarWebServiceImplv4_1_custom->example_method.');
return false;
}
return $session;
}
}
?>
定义注册表Class
接下来,我们将定义注册表 class 来注册我们的新功能。该文件将位于:
./custom/service/v4_1_custom/registry.php
<?php
require_once('service/v4_1/registry.php');
class registry_v4_1_custom extends registry_v4_1
{
protected function registerFunction()
{
parent::registerFunction();
$this->serviceClass->registerFunction('example_method', array('session'=>'xsd:string'), array('return'=>'xsd:string'));
}
}
?>
定义 REST 入口点
此 REST 入口点将位于:
./custom/service/v4_1_custom/rest.php
<?php
chdir('../../..');
require_once('SugarWebServiceImplv4_1_custom.php');
$webservice_path = 'service/core/SugarRestService.php';
$webservice_class = 'SugarRestService';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$registry_class = 'registry_v4_1_custom';
$location = 'custom/service/v4_1_custom/rest.php';
require_once('service/core/webservice.php');
?>
定义 SOAP 入口点
此 SOAP 入口点将位于:
./custom/service/v4_1_custom/soap.php
<?php
chdir('../../..');
require_once('SugarWebServiceImplv4_1_custom.php');
$webservice_class = 'SugarSoapService2';
$webservice_path = 'service/v2/SugarSoapService2.php';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_class = 'registry_v4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$location = 'custom/service/v4_1_custom/soap.php';
require_once('service/core/webservice.php');
?>
使用 API 获取模块数据
您好,我使用 API 获取模块数据,我已经实现了很多 APIS 来获取数据。
你必须在
中写这个API
custom\clients\base\api\filename.php
<?php
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Origin:", "*");
if (!defined('sugarEntry') || !sugarEntry)
die('Not A Valid Entry Point');
require_once('include/SugarOAuth2/SugarOAuth2Server.php');
require_once('custom/include/api/CustomApiException.php');
class CustomApi extends ModuleApi {
const CustomPortalAPIUser = 'b6ab0b1c-b371-0bb9-6653-570f2a0a5b47';
/** @var RelateRecordApi */
public function registerApiRest() {
return array(
'cRetrieve' => array(
'reqType' => 'GET',
'path' => array('portal', '<module>', '?'),
'pathVars' => array('portal', 'module', 'record'),
'method' => 'customRetrieveRecord',
'shortHelp' => 'Returns a single record',
'longHelp' => 'include/api/help/module_record_get_help.html',
),
);
}
public function customAuthenticateUser($api, $args) {
if (isset($api->request_headers['OAUTH_TOKEN'])) {
$oauthServer = SugarOAuth2Server::getOAuth2Server();
$oauthServer->verifyAccessToken($api->request_headers['OAUTH_TOKEN']);
//$args_user['platform'] = $api->request_headers['OAUTH_TOKEN'];
} else {
//$args_user['platform'] = "base";
}
global $fields;
require_once('clients/base/api/OAuth2Api.php');
require_once('include/SugarOAuth2/SugarOAuth2Server.php');
$args_user['platform'] = "base";
$args_user['username'] = 'username';
$args_user['password'] = 'password';
$args_user['client_id'] = 'sugar';
$args_user['grant_type'] = 'password';
$args_user['client_secret'] = '';
$args_user['current_language'] = 'en_us';
$oAuth = new OAuth2Api();
$token_data = $oAuth->token($api, $args_user);
$admin = Administration::getSettings();
if (isset($admin->settings['portal_defaultUser']) && !empty($admin->settings['portal_defaultUser'])) {
$fields['assigned_user_id'] = json_decode(html_entity_decode($admin->settings['portal_defaultUser']));
}
return $token_data;
}
public function customRetrieveRecord($api, $args) {
$is_authenticate = $this->customAuthenticateUser($api, $args);
$result = parent::retrieveRecord($api, $args);
if ($args['module'] == 'Module_name') {
$result['data'] = self::data;
your code and logic ....
}
return $result;
}
}
$entryArgs = array(
'session' => $sessId,
'module_name' => 'SecurityGroups',
'query' => "securitygroups_users.securitygroup_id = 'id`enter code
here`'",
'order_by' => '',
'offset' => 0,
'select_fields' => array('id','name'),
'max_results' => 10,
'deleted' => 0 );
$module_result = restRequest('get_entry_list',$entryArgs);
echo "<pre>";
print_r($module_result);
echo "</pre>";
我想通过 REST API 从套件 CRM securitygroup_users table 中获取数据。但我什么也没得到。任何帮助将不胜感激。
我想写一个自定义的 Rest API 你可以按照这个 link Custom Rest API
扩展 Web 服务 概述
本指南将演示如何将您自己的自定义方法添加到 REST 和 SOAP API 或扩展现有方法。 扩展 API
下面的例子将演示如何扩展v4_1API。 定义入口点位置
您可以在此处定义包含新 REST 和 SOAP 入口点的目录。我们推荐的路径格式如下:
./custom/service/{version}_custom/
入口点的实际位置并不重要,但是,使用这样的路径将允许您按如下方式调用入口点:
http://{sugar_url}/custom/service/{version}_custom/rest.php http://{sugar_url}/custom/service/{version}_custom/soap.php
定义 SugarWebServiceImpl Class
下一步是定义一个新的“SugarWebServiceImpl
”class。由于我们正在使用 v4_1,因此我们需要扩展文件 'service/v4_1/SugarWebServiceImplv4_1.php'
并添加我们的新方法。为此,我们将创建文件:
./custom/service/v4_1_custom/SugarWebServiceImplv4_1_custom.php
<?php
if(!defined('sugarEntry'))define('sugarEntry', true);
require_once('service/v4_1/SugarWebServiceImplv4_1.php');
class SugarWebServiceImplv4_1_custom extends SugarWebServiceImplv4_1
{
/*
* Returns the session id if authenticated
*
* @param string $session
* @return string $session - false if invalid.
*
*/
function example_method($session)
{
$GLOBALS['log']->info('Begin: SugarWebServiceImplv4_1_custom->example_method');
$error = new SoapError();
//authenticate
if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error))
{
$GLOBALS['log']->info('End: SugarWebServiceImplv4_1_custom->example_method.');
return false;
}
return $session;
}
}
?>
定义注册表Class
接下来,我们将定义注册表 class 来注册我们的新功能。该文件将位于:
./custom/service/v4_1_custom/registry.php
<?php
require_once('service/v4_1/registry.php');
class registry_v4_1_custom extends registry_v4_1
{
protected function registerFunction()
{
parent::registerFunction();
$this->serviceClass->registerFunction('example_method', array('session'=>'xsd:string'), array('return'=>'xsd:string'));
}
}
?>
定义 REST 入口点
此 REST 入口点将位于:
./custom/service/v4_1_custom/rest.php
<?php
chdir('../../..');
require_once('SugarWebServiceImplv4_1_custom.php');
$webservice_path = 'service/core/SugarRestService.php';
$webservice_class = 'SugarRestService';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$registry_class = 'registry_v4_1_custom';
$location = 'custom/service/v4_1_custom/rest.php';
require_once('service/core/webservice.php');
?>
定义 SOAP 入口点
此 SOAP 入口点将位于:
./custom/service/v4_1_custom/soap.php
<?php
chdir('../../..');
require_once('SugarWebServiceImplv4_1_custom.php');
$webservice_class = 'SugarSoapService2';
$webservice_path = 'service/v2/SugarSoapService2.php';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_class = 'registry_v4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$location = 'custom/service/v4_1_custom/soap.php';
require_once('service/core/webservice.php');
?>
使用 API 获取模块数据 您好,我使用 API 获取模块数据,我已经实现了很多 APIS 来获取数据。
你必须在
中写这个APIcustom\clients\base\api\filename.php
<?php
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Origin:", "*");
if (!defined('sugarEntry') || !sugarEntry)
die('Not A Valid Entry Point');
require_once('include/SugarOAuth2/SugarOAuth2Server.php');
require_once('custom/include/api/CustomApiException.php');
class CustomApi extends ModuleApi {
const CustomPortalAPIUser = 'b6ab0b1c-b371-0bb9-6653-570f2a0a5b47';
/** @var RelateRecordApi */
public function registerApiRest() {
return array(
'cRetrieve' => array(
'reqType' => 'GET',
'path' => array('portal', '<module>', '?'),
'pathVars' => array('portal', 'module', 'record'),
'method' => 'customRetrieveRecord',
'shortHelp' => 'Returns a single record',
'longHelp' => 'include/api/help/module_record_get_help.html',
),
);
}
public function customAuthenticateUser($api, $args) {
if (isset($api->request_headers['OAUTH_TOKEN'])) {
$oauthServer = SugarOAuth2Server::getOAuth2Server();
$oauthServer->verifyAccessToken($api->request_headers['OAUTH_TOKEN']);
//$args_user['platform'] = $api->request_headers['OAUTH_TOKEN'];
} else {
//$args_user['platform'] = "base";
}
global $fields;
require_once('clients/base/api/OAuth2Api.php');
require_once('include/SugarOAuth2/SugarOAuth2Server.php');
$args_user['platform'] = "base";
$args_user['username'] = 'username';
$args_user['password'] = 'password';
$args_user['client_id'] = 'sugar';
$args_user['grant_type'] = 'password';
$args_user['client_secret'] = '';
$args_user['current_language'] = 'en_us';
$oAuth = new OAuth2Api();
$token_data = $oAuth->token($api, $args_user);
$admin = Administration::getSettings();
if (isset($admin->settings['portal_defaultUser']) && !empty($admin->settings['portal_defaultUser'])) {
$fields['assigned_user_id'] = json_decode(html_entity_decode($admin->settings['portal_defaultUser']));
}
return $token_data;
}
public function customRetrieveRecord($api, $args) {
$is_authenticate = $this->customAuthenticateUser($api, $args);
$result = parent::retrieveRecord($api, $args);
if ($args['module'] == 'Module_name') {
$result['data'] = self::data;
your code and logic ....
}
return $result;
}
}