sugarcrm

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

什么是REST?

常用缩略语
  • API:应用程序编程接口
  • CSS:级联样式表
  • DOM:文档对象模型
  • HTML:超文本标记语言
  • HTTP:超文本传输​​协议
  • JSON:JavaScript对象表示法
  • REST:代表性状态转移
  • RPC:远程过程调用
  • URL:统一资源定位符
  • XML:可扩展标记语言

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

sugarcrm_通过SugarCRM休息相关推荐

  1. 正确“假期休息模式”

    休息的真正含义是什么?是恢复疲劳,放松神经,当你重新投入工作与学习的时候觉得又是一个精力充沛的新人. 最好的休息,是让你重燃生活的热情. 为什么你睡了11个小时仍然觉得疲累?为什么你花了好几万去国外度 ...

  2. 批处理+定时任务实现定时休息提醒

    前言:俗话说的好,懒是第一生产力,懒是提高生产效率的必要条件.而现今windows是大部分人的第一生产工具,批处理+定时任务这对黄金搭档就是提升生产效率的第一工具.大家在生产过程中经常会遇到各种周期性 ...

  3. 周末不用过来了,好好休息吧_如何好好休息

    周末不用过来了,好好休息吧 When I wrote about my productive routine in a previous article, I said I'd work for 1. ...

  4. 导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....

    点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文来源:转自科学网博客 王德华译 研究生从大学生生活转换到研究生生 ...

  5. 极客新闻——02、最高效的人都是最会休息的人

    本文笔记全部来自<极客新闻> 压力是现代生活中无法回避的话题,最优秀的领导者们并不是没有压力,他们只是比大部分人都懂得如何应对压力.保持最佳状态. 如果一个时间段内只做一件事,往往每件事都 ...

  6. 阿里某新员工感慨:入职阿里三个月生活一团糟,想辞职休息

    来自:百家号-职场坊间大小事 去科技大厂上班看似风光无限,实则处处充满着心酸史,近日在职场论坛,阿里某新员工发帖诉说自己在阿里三个月的经历:才入职阿里三个月,刚转正,试用期绩效3.5,最近心态有点不好 ...

  7. 第一:做学位论文期间,不要有任何度假休息的打算;第二,导师错的时候不多;第三……...

    内容来源:科学网博客,作者王德华 研究生从大学生生活转换到研究生生活,在思想意识上,学习和工作方法上,都需要进行一些改变.如果转变不好,会严重影响研究生阶段的学习和工作.研究生阶段已经再也不是以考试成 ...

  8. 脑科学研究:对于学习来说,休息可能与练习同样重要...

    来源:神经科技 近日,在针对健康志愿者的的一项研究中,美国国立卫生研究院(NIH)的研究人员发现,大脑可能会通过短暂的休息来巩固我们几秒钟前刚练习过的新技能的记忆.该研究结果强调了早期休息在学习中可能 ...

  9. 你的 webpack 也需要休息,让你的 webpack 拒绝 996ICU

    最近 github 上的 996ICU 项目很火,IT 从业者们都纷纷呼吁拒绝 996ICU,但是你有没有想过,你加班的时候,你干活的工具也在陪你加班呢,它们的感受你想过吗?它们也需要休息,它们也会生 ...

最新文章

  1. 报告:最大化人工智能(AI)机遇
  2. MDK5.29,5.30,5.31,5.32,5.33和各种pack软件包镜像下载
  3. 【学习笔记】出货认证(QM模块)
  4. 一个简单的c++/cli中委托与事件的处理模型
  5. 项目管理、bug管理工具 ---禅道使用流程
  6. LocED-Location-aware Energy Disggregation Framework
  7. excel文件下载下来损坏 js_js实现txt/excel文件下载
  8. 常见后端数据存储问题解决方案
  9. javascript 获取汉字笔画拼音,使用笔画排序
  10. systemd服务分析
  11. 【记vue项目中的踩坑日记】一杯茶一包烟,一个bug搞一天
  12. Web测试要点(功能、性能、可用性、兼容、安全)
  13. pandas基础操作大全之数据合并
  14. 【云驻共创】华为云之锁与权限为您的数仓保驾护航
  15. 自己动手、丰衣足食!箭头 → ← → ← ---2
  16. 小白学java之车辆管理系统,超基础!
  17. 山东教师教育网研修平台-首页
  18. 北京朝阳一互联网公司被端,23人被警方带走…这种开发千万别干!
  19. 在Excel VBA中使用字典
  20. Spring Data MongoDB SpEL表达式注入漏洞安全风险通告第二次更新

热门文章

  1. 合并石子问题研究与实现
  2. Android5.0 Alarm服务简单分析
  3. oracle pls 00905,ORA-06550:line 1;column 7;pls-00103
  4. Flutter学习(一) 状态管理
  5. 工行企业网银“您的数据签名有误请联系当地工行”解决办法
  6. Ceilometer Neutron-Metering
  7. SpringBoot+2次MD5登录密码加密+MyBatisPlus+Thymeleaf+Bootstrap简单实现登录功能,一文轻松搞定!
  8. 模拟WSockExpert,使用APIHOOK
  9. 2分钟上手、3小时学会无代码软件开发---日期、时间和计时器
  10. 你的交际力能力好吗?