SuiteCRM开发文档 API v4.1

注意: 本文档适用于v.7.9.x之前的SuiteCRM版本。自SuiteCRM 7.10版本起,全新的v8 REST API 可用。

SuiteCRM API允许第三方代码访问和编辑SuiteCRM数据和功能。

使用API 

SuiteCRM同时具有REST和SOAP API。无论您要使用哪种语言,要使用哪种API都很大程度上取决于个人喜好以及对SOAP / REST库的支持。

这两个API均需要用户名和密码。通常会专门为该API创建一个用户。

可以在以下位置找到SOAP API的WSDL:示例16.1:SOAP API WSDL位置

example.com/suitecrm/service/v4_1/soap.php?wsdl

哪里 example.com/suitecrm是SuiteCRM实例的地址。 v4_1是API的版本,可以更改。

SOAP示例

以下PHP示例使用内置的SoapClient类。

例16.2:访问SOAP API

<?php
//Create a new SoapClient
$wsdlURL = "http://example.com/suitecrm/service/v4_1/soap.php?wsdl";
$client = new SoapClient($wsdlURL);

//Login to the API and get the session id
$userAuth = array(
        'user_name' => '<suitecrmuser>',
        'password' => md5('<suitecrmpassword>'),
);
$appName = 'My SuiteCRM SOAP Client';
$nameValueList = array();
$loginResults = $client->login($userAuth, $appName, $nameValueList);

//Get a list of at most 10 accounts with a billing address in Ohio. Along with
//The first and last names of any contacts in that Account.
$results = $client->get_entry_list(
        //Session id - retrieved from login call
        $loginResults->id,
        //Module to get_entry_list for
        'Accounts',
        //Filter query - Added to the SQL where clause
        "accounts.billing_address_city = 'Ohio'",
        //Order by - unused
        '',
        //Start with the first record

        //Return the id and name fields
        array('id','name'),
        //Link to the "contacts" relationship and retrieve the
        //First and last names.
        array(
                array(
                        'name' => 'contacts',
                        'value' => array(
                                'first_name',
                                'last_name',
                        ),
                ),
        ),
        //Show 10 max results

        //Do not show deleted

);
print_r($results);

REST

可在以下位置找到SuiteCRM REST API:例16.3:REST API端点位置

example.com/suitecrm/service/v4_1/rest.php

哪里 example.com/suitecrm是SuiteCRM实例的地址。 v4_1是API的版本,可以更改。

SuiteCRM REST API并不是真正的REST API-所有调用均作为POST执行,并且所有调用均以作为post参数传入的方法对基本URL进行。

REST API调用的参数为:method

将被调用的方法,即loginget_entry_list。看到 API v4.1方法列表input_type

rest_data的输入类型。这通常是,JSON但也可以是Serializeresponse_type

响应如何编码。这通常是,JSON但也可以是Serializerest_data

此方法所需的任何其他参数。这作为编码数组传递。编码由input_type确定。

注意:请注意,对于REST请求,重要的是参数的顺序 rest_data 而不是名字。

例16.4:访问REST API

<?php

$url = "http://example.com/suitecrm/service/v4_1/rest.php";

function restRequest($method, $arguments){
 global $url;
 $curl = curl_init($url);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 $post = array(
         "method" => $method,
         "input_type" => "JSON",
         "response_type" => "JSON",
         "rest_data" => json_encode($arguments),
 );

 curl_setopt($curl, CURLOPT_POSTFIELDS, $post);

 $result = curl_exec($curl);
 curl_close($curl);
 return json_decode($result,1);
}


$userAuth = array(
        'user_name' => 'suitecrmuser',
        'password' => md5('suitecrmpassword'),
);
$appName = 'My SuiteCRM REST Client';
$nameValueList = array();

$args = array(
            'user_auth' => $userAuth,
            'application_name' => $appName,
            'name_value_list' => $nameValueList);

$result = restRequest('login',$args);
$sessId = $result['id'];

$entryArgs = array(
 //Session id - retrieved from login call
	'session' => $sessId,
 //Module to get_entry_list for
	'module_name' => 'Accounts',
 //Filter query - Added to the SQL where clause,
	'query' => "accounts.billing_address_city = 'Ohio'",
 //Order by - unused
	'order_by' => '',
 //Start with the first record
	'offset' => 0,
 //Return the id and name fields
	'select_fields' => array('id','name',),
 //Link to the "contacts" relationship and retrieve the
 //First and last names.
	'link_name_to_fields_array' => array(
        array(
                'name' => 'contacts',
                        'value' => array(
                        'first_name',
                        'last_name',
                ),
        ),
),
   //Show 10 max results
  		'max_results' => 10,
   //Do not show deleted
  		'deleted' => 0,
 );
 $result = restRequest('get_entry_list',$entryArgs);

print_r($result);

有关API方法及其参数的完整列表,请参见 这里

添加自定义API方法 

有时,现有的API方法不够用,或者将它们用于任务可能会过于复杂。SuiteCRM允许使用其他方法或覆盖现有方法来扩展Web服务。

自定义入口点的建议路径如下 custom/service/<version>_custom/。对于Web服务版本,v4_1这将是custom/service/v4_1_custom/

接下来,我们创建实现类。这将创建我们的新方法。在我们的示例中,我们将简单地创建一个写入SuiteCRM日志的新方法。我们称这种方法write_log_message

例子

示例16.5:定制v4_1 Web服务实现

<?php
if(!defined('sugarEntry')){
  define('sugarEntry', true);
}
require_once 'service/v4_1/SugarWebServiceImplv4_1.php';
class SugarWebServiceImplv4_1_custom extends SugarWebServiceImplv4_1
{

  function write_log_message($session, $message)
  {
    $GLOBALS['log']->info('Begin: write_log_message');

    //Here we check that $session represents a valid session
    if (!self::$helperObject->checkSessionAndModuleAccess(
                                                    $session,
                                                    'invalid_session',
                                                    '',
                                                    '',
                                                    '',
                                                    new SoapError()))
    {
      $GLOBALS['log']->info('End: write_log_message.');
      return false;
    }
    $GLOBALS['log']->info($message);
    return true;
  }
}

接下来,我们创建注册表文件,该文件将注册我们的新方法。

示例16.6:自定义v4_1 Web服务注册表

<?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('write_log_message',
                                                  array(
                                                    'session'=>'xsd:string',
                                                    'message'=>'xsd:string'),
                                                  array(
                                                    'return'=>'xsd:boolean')
                                                  );
        }
    }

最后,我们创建入口点。这是我们的API客户端将调用的实际文件。这将引用我们创建的两个文件,并使用我们的文件调用Webservice实现。

示例16.7:自定义v4_1 REST入口点

<?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';

示例16.8:自定义v4_1 SOAP入口点

<?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');

用法 

现在,我们可以使用我们的自定义端点。除了我们将自定义入口点用于SOAP WSDL或REST URL外,这与使用上述API相同。例如,使用相同的SuiteCRM位置(example.com/suitecrm)作为上述示例并使用v4_1,我们将使用以下

示例示例16.9:自定义v4_1 URL

//SOAP WSDL
 example.com/suitecrm/custom/service/v4_1_custom/soap.php?wsdl
 //REST URL
 example.com/suitecrm/custom/service/v4_1_custom/rest.php

推荐阅读

  1. SuiteCRM 8.1.2 刚刚发布!
  2. SuiteCRM 8.1.1 & 7.12.6 发布
  3. 为了官方更好的技术服务,请捐赠SuiteCRM
  4. SuiteCRM 8.1发布-官方版本再更新!
  5. 苏格兰公司 SuiteCRM 再次获奖
  6. 安全和维护补丁:SuiteCRM 8.0.4 & 7.12.5 发布
  7. 将 SuiteCRM 升级到更新版本
  8. SuiteCRM 升级环境(PHP\MySQL\Apache)要求

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注