sugarcrm_通过SugarCRM休息

sugarcrm

2010年2月2日-作者在2010年2月2日增加了关于SugarCRM连接器的资源项目。

什么是REST?

REST(代表表示状态转移)被设计为一种非常卑鄙的精益Web服务协议。 它是对诸如SOAP和XML-RPC之类的重量级Web服务协议的一种响应,这些协议依赖于预定义的消息传递格式和方法在服务器和客户端之间来回传递它们。 另一方面,REST未指定此类限制。 您可以使用自己喜欢的任何消息格式(无论是JSON,XML,HTML,序列化数据,甚至是纯文本),并以GETDELETEPOST的标准HTTP动词进行操作。 REST客户端/服务器交互存在的规则可以由应用程序需求完全定义。 因此,如果您定义了一个打算供JavaScript客户端使用的REST接口,则您可能希望以JSON格式返回数据,而如果您打算由PHP客户端使用该数据,则可能要序列化数据或XML是更好的选择。

每个REST Web服务调用都是使用标准HTTP动词之一的简单HTTP请求。 通常,您将使用最适合您执行的动作的动词:

  • GET从服务器检索数据
  • POST从服务器发送数据
  • DELETE以删除服务器上的资源

清单1显示了如何与Yahoo!交互的示例。 使用PHP搜索REST Web服务是可行的(请参见可下载资源 ,使本文中的所有示例都唾手可得)。

清单1.与PHP中的REST Web服务进行交互的示例
<?php 
// specify the REST web service to interact with 
$url = 'http://search.yahooapis.com/WebSearchService/V1/
     webSearch?appid=YahooDemo&query=sugarcrm'; 
// make the web services call; the results are returned in XML format 
$resultsXml = file_get_contents($url); 
// convert XML to a SimpleXML object 
$xmlObject = simplexml_load_string($resultsXml); 
// iterate over the object to get the title of each search result 
foreach ( $xmlObject->Result as $result ) 
    echo "{$result->Title}\n";

对于清单1中的示例,您正在使用Yahoo Web Search Service搜索sugarcrm。 您使用PHP函数file_get_contents() ,该函数使用指定的URL执行GET请求。 默认情况下,它以XML字符串的形式返回查询的结果,并且使用PHP SimpleXML库将其解析为一个PHP对象,您可以对其进行迭代以获取所需的数据。

现在您已经了解了REST Web服务的工作原理,然后了解如何使用其Web服务界面与SugarCRM进行交互。

使用REST连接到SugarCRM

SugarCRM随附的Web服务接口可以立即使用,位于http:// path_to_Sugar_instance /v2/rest.php。 使用对此URL的POST请求进行调用,并将所需的参数作为POST参数传递给Web服务调用。 对于与Web服务的每次交互,客户端首先必须使用login方法调用向服务进行身份验证,如清单2所示

清单2.通过REST Web服务界面登录到SugarCRM实例
<?php 
// specify the REST web service to interact with 
$url = 'http://localhost/sugar/v2/rest.php'; 
// Open a curl session for making the call 
$curl = curl_init($url); 
// Tell curl to use HTTP POST 
curl_setopt($curl, CURLOPT_POST, true); 
// Tell curl not to return headers, but do return the response 
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
// Set the POST arguments to pass to the Sugar server 
$parameters = array( 
    'user_name' => 'user', 
    'password' => 'password', 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=login&input_type=json&
      response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Close the connection 
curl_close($session);  
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Echo out the session id 
echo $result['id'];

由于您需要向Web服务发出POST请求而不是GET请求,因此请使用PHP curl库而不是file_get_contents()来调用Sugar Web服务。 然后,将Web服务调用的参数组装为数组,并编码为JSON字符串。 您可以使用input_type POST参数(可以是jsonserialized )将指定此Web服务调用的参数,方法(此调用的login )以及用于将参数传递给服务的格式。 您还必须指定期望返回结果的格式(如response_type参数(可以是jsonrssserialized参数之一)),然后将Web服务方法的JSON编码参数字符串指定为rest_data 。 然后,您进行实际的调用,并解码从服务器传递来的返回的JSON字符串。 您为此请求关注的主要返回值是id参数,该参数在后续请求中用作参数,以向您的服务器标识您已通过该服务的身份验证。

建立新纪录

现在,您可以基于清单2的示例,对Sugar Web服务进行实际更改。 在清单3中 ,您将看到如何使用该界面将新记录添加到Accounts模块。

清单3.使用REST Web服务界面添加一个新帐户
<?php 
// specify the REST web service to interact with 
$url = 'http://localhost/sugar/v2/rest.php'; 
// Open a curl session for making the call 
$curl = curl_init($url); 
// Tell curl to use HTTP POST 
curl_setopt($curl, CURLOPT_POST, true); 
// Tell curl not to return headers, but do return the response 
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
// Set the POST arguments to pass to the Sugar server 
$parameters = array( 
    'user_name' => 'user', 
    'password' => 'password', 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=login&input_type=json&
       response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Close the connection 
curl_close($session);  
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the session id 
$sessionId = $result['id']; 
// Now, let's add a new Accounts record 
$parameters = array( 
    'session' => $session, 
    'module' => 'Accounts', 
    'name_value_list' => array( 
        array('name' => 'name', 'value' => 'New Account'), 
        array('name' => 'description', 'value' => 'This is an 
account created from a REST web services call'), 
        ), 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=set_entry&input_type=json&
     response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the newly created record id
$recordId = $result['id'];

登录到Web服务之后,您将再次调用Web服务,这次是set_entry Web服务方法。 您可以将login方法返回的会话ID指定为session参数,并将要添加记录的module指定为module参数。 name_value_list参数是您希望为新创建的记录设置的字段值的名称/值对列表。 然后,您进行Web服务调用,该服务返回新创建的记录的记录ID。

您还可以通过再次调用相同的set_entry方法来更新一条记录,确保在name_value_pair列表中传递您想要更新的记录id,如清单4所示

清单4.使用REST Web服务界面创建和更新联系人
<?php 
// specify the REST web service to interact with 
$url = 'http://localhost/sugar/v2/rest.php'; 
// Open a curl session for making the call 
$curl = curl_init($url); 
// Tell curl to use HTTP POST 
curl_setopt($curl, CURLOPT_POST, true); 
// Tell curl not to return headers, but do return the response 
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
// Set the POST arguments to pass to the Sugar server 
$parameters = array( 
    'user_name' => 'user', 
    'password' => 'password', 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=login&input_type=json&
    response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Close the connection 
curl_close($session);  
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the session id 
$sessionId = $result['id']; 
// Now, let's add a new Contacts record 
$parameters = array( 
    'session' => $session, 
    'module' => 'Contacts', 
    'name_value_list' => array( 
        array('name' => 'first_name', 'value' => 'John'), 
        array('name' => 'last_name', 'value' => 'Mertic'), 
        ), 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=set_entry&input_type=json&
    response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the newly created record id 
$recordId = $result['id']; 
// Now let's update that record we just created 
$parameters = array( 
    'session' => $session, 
    'module' => 'Contacts', 
    'name_value_list' => array( 
        array('name' => 'id', 'value' => $recordId), 
        array('name' => 'title', 'value' => 'Engineer'), 
        ), 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=set_entry&input_type=json&
     response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the record id of the record we just updated 
$recordId = $result['id'];

在这个清单4的示例中,您只需进行第二个set_entry方法调用来更新新创建的记录。 这次您指定要更新的记录的记录ID作为Web服务方法调用的name_value_list参数中的条目,并为要更新的每个字段提供一个条目作为调用的一部分(对于本例,您将正在将title字段更新为值Engineer )。 然后,您发出请求,并查找记录ID以作为Web服务方法调用成功结果的标志返回给您。

创建多个记录

如果要在一个模块中创建多个记录,则可以使用Web服务方法set_entries Web服务API的调用次数减少到一个,而不是在循环中调用set_entry清单5显示了如何完成此工作。

清单5.在一个方法调用中在一个模块中创建多个记录
<?php 
// specify the REST web service to interact with 
$url = 'http://localhost/sugar/v2/rest.php'; 
// Open a curl session for making the call 
$curl = curl_init($url); 
// Tell curl to use HTTP POST 
curl_setopt($curl, CURLOPT_POST, true); 
// Tell curl not to return headers, but do return the response 
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
// Set the POST arguments to pass to the Sugar server 
$parameters = array( 
    'user_name' => 'user', 
    'password' => 'password', 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=login&input_type=json&
     response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Close the connection 
curl_close($session);  
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the session id 
$sessionId = $result['id']; 
// Now, let's add a new Contacts record 
$parameters = array( 
    'session' => $session, 
    'module' => 'Contacts', 
    'name_value_lists' =>  
        array( 
            array('name' => 'first_name', 'value' => 'John'), 
            array('name' => 'last_name', 'value' => 'Mertic'), 
        ), 
        array( 
            array('name' => 'first_name', 'value' => 'Dominic'), 
            array('name' => 'last_name', 'value' => 'Mertic'), 
        ), 
        array( 
            array('name' => 'first_name', 'value' => 'Mallory'), 
            array('name' => 'last_name', 'value' => 'Mertic'), 
        ), 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=set_entry&input_type=json&
     response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the newly created record ids as an array 
$recordIds = $result['ids'];

对于参数set_entries是几乎相同set_entry ,除了没有name_value_list ,参数是name_value_lists ,这是您创建使用Web服务API记录的关联数组。 创建的记录的记录id以数组的形式返回到ids参数中,并以它们在参数列表中传递的顺序返回。

将记录关联在一起

SugarCRM的一个主要概念是将记录彼此关联的能力。 这种关系的一个例子是客户和联系人之间。 SugarCRM中的每个帐户可以具有一个或多个与其相关的联系人。 您可以完全使用Web服务框架来建立这种关系,如清单6所示

清单6.使用REST Web服务API将帐户与联系人相关联
<?php 
// specify the REST web service to interact with 
$url = 'http://localhost/sugar/v2/rest.php'; 
// Open a curl session for making the call 
$curl = curl_init($url); 
// Tell curl to use HTTP POST 
curl_setopt($curl, CURLOPT_POST, true); 
// Tell curl not to return headers, but do return the response 
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
// Set the POST arguments to pass to the Sugar server 
$parameters = array( 
    'user_name' => 'user', 
    'password' => 'password', 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=login&input_type=json&
      response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Close the connection 
curl_close($session);  
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the session id 
$sessionId = $result['id']; 
// Now, let's add a new Accounts record 
$parameters = array( 
    'session' => $session, 
    'module' => 'Accounts', 
    'name_value_list' => array( 
        array('name' => 'name', 'value' => 'New Account'), 
        array('name' => 'description', 'value' => 'This is an 
account created from a REST web services call'), 
        ), 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=set_entry&input_type=json&
    response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the newly created Account record id 
$accountId = $result['id']; 
// Now, let's add a new Contacts record 
$parameters = array( 
    'session' => $session, 
    'module' => 'Contacts', 
    'name_value_list' => array( 
        array('name' => 'first_name', 'value' => 'John'), 
        array('name' => 'last_name', 'value' => 'Mertic'), 
        ), 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=set_entry&input_type=json&
       response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call, returning the result 
$response = curl_exec($session); 
// Convert the result from JSON format to a PHP array 
$result = json_decode($response); 
// Get the newly created Contact record id 
$contactId = $result['id']; 
// Now let's relate the records together 
$parameters = array( 
    'session' => $session, 
    'module_id' => $accountId 
    'module_name' => 'Accounts', 
    'link_field_name' => 'contacts', 
    'related_ids' => array($contactId), 
    ); 
$json = json_encode($parameters); 
$postArgs = 'method=set_relationship&input_type=json&
    response_type=json&rest_data=' . $json; 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
// Make the REST call 
$response = curl_exec($session);

在创建了您希望一起关联的Account和Contact之后,您将构建对set_relationship Web服务方法调用的请求。 然后,您传递以下内容:

  • Web服务会话的会话ID的session参数
  • 该关系的主模块的module_name
  • module_id为该模块中记录的ID,将成为关系的基础
  • link_field_name表示该模块中用于链接到另一个模块的关系的名称(在这种情况下,该关系称为contacts

最后,然后指定与指定模块记录相关的记录ID列表。 拨打电话后,将建立关系。

这只是可用的Web服务方法的冰山一角。 请务必查看SugarCRM开发人员文档以获取可用Web服务方法的完整列表。

摘要

在本文中,您了解了SugarCRM 5.5中的新功能,它是Sugar Web Services框架的REST接口。 在了解了REST Web服务的工作原理后,您了解了一些利用REST接口到Sugar Web服务框架的示例。 您了解了如何向模块添加新记录,然后如何修改该记录。 您还看到了如何在一个方法调用中添加多个记录,从而节省了连续进行多个远程服务器调用的开销。 最后,您了解了如何使用Web服务框架的set_relationship方法将两个不同的记录关联在一起。


翻译自: https://www.ibm.com/developerworks/opensource/library/x-sugarcrmrest/index.html

sugarcrm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,感谢 索孚方科 公司的大力支持以及相关研发人员的鼎力协助,让山寨版的CE语言包可能比Pro版的更贴切好用一些,故本语言包也可以说攀了个粗腿。对公司相关人员快捷高效的测试,表示感谢! 其次,注意事项: 兼容6.5.X; 一般注意事项,见http://down.51cto.com/data/275922 建议中文的sugar系统修改config.php配置文件,以获得更好使用(先备份好原文 件)。 config.phpsugar配置文件,建议更改部分。 第1处: 'default_currency_iso4217' => 'CNY', //修改默认货币代码为人民币 'default_currency_name' => 'Chinese Yuan', //修改默认货币名称为元,可在 系统管理->货币 下增加汇率和其他货币 第2处: 'default_language' => 'zh_cn', //修改默认登陆为中文 第3处: 'default_locale_name_format' => 's f l', //姓名称谓 第4处: 'default_charset' => 'EUC-CN', //导入导出为GB2312格式,避免中文乱码 第5处,可不变: 'upload_dir' => 'upload/', //保持此目录或者修改上传目录,包括document的文件附件存放在此目录 'upload_maxsize' => 30000000, //保持或者修改此值,修改单个文件最大上传值(默认30M) =========================================================================== 本次语言包解决的一些问题: 1.在IE下的Ajax问题(模块因语言问题显示null),详见《6.4.3中文模块列表不能正确显示?寻帮助》一文http://www.sugar360.cn/forum.php ... &extra=page=1 2.已解决:模块生成器->文件包-> 可用子面板->默认,点开后无内容(而英文下无此问题); 3.中文下,日历的日期与星期几可能不对应的问题; 4.更新翻译,让其更加易懂。 5.相关翻译人员:木子飞飞 (rell336@126.com),无常(Huang.jecky@gmail.com),Cheli Zhao (cheli.zhao@srforce.com), Richard Qi (richard@srforce.com)。 6.若对翻译的一些文字有建议,可直接联系我:rell336@126.com 14:52 2014-7-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值