未加星标

Using the Sugar REST PHP Client

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
Sugar RESTphp Client

A new open source library for working with Sugar 7’s powerful REST API has just been published! You can view the library in our GitHub account here: https://github.com/sugarcrm/rest-php-client

Full instructions for installation, usage, current included API Endpoints, and ways to contribute can all be found in the GitHub Wiki on the repository.

Who should use it?

The Sugar REST PHP Client was built initially to make working with Sugar instances easier for some of our internal tools. I wanted to provide a quick, easy, and object oriented, way to access Sugar 7’s REST API that could be used by all PHP developers. Any developer who is developing PHP applications that integrate with Sugar 7 should be interested in using the new library. The simplified architecture takes away the hassle of setting up and managing Curl connections, and allows the developer to focus on what matters most, which is working with the data in their Sugar application.

How to use it?

The REST PHP Client is setup as a composer package, and can be easily added to your existing project by adding it to your required packages in your projects composer.json file.

Once it’s installed via composer, you can access the Client in the SugarAPI\SDK namespace.

A quick example of how much easier to use this new library is, can be shown by reviewing the latest documentation on using the REST API to get a filtered list of records from a module in the Support Developer Guide , and comparing it to the following snippet of code that does the exact same API request.

$server = 'localhost'; $credentials = array( 'username' => 'admin', 'password' => 'asdf' ); $SugarAPI = new \SugarAPI\SDK\SugarAPI($server, $credentials); $SugarAPI->login(); $requestData = array( "filter" => array( array( '$or' => array( array( //name starts with 'a' "name" => array( '$starts'=>"A", ) ), array( //name starts with 'b' "name" => array( '$starts'=>"b", ) ) ), ), ), "max_num" => 2, "offset" => 0, "fields" => "id", "order_by" => "date_entered", "favorites" => false, "my_items" => false, ); $response = $SugarAPI->filterRecords('Accounts') ->execute($requestData) ->getResponse(); if ($response->getStatus()=='200'){ $records = $response->getBody(); print_r($records); }

Each API Endpoint in the Sugar 7 REST API is defined by an Endpoint Class in the REST PHP Client library, which is then dynamically mapped to a method on the REST PHP Client Object. This dynamic method, generates the Endpoint Object, configures the Curl Request, and returns the Endpoint Object for manipulation, such as loading in data and finally executing the actual request.

/** Calling filterRecords() dynamic method, returns a SugarAPI\SDK\Endpoint\POST\ModuleFilter Object **/ $SugarAPI = new \SugarAPI\SDK\SugarAPI($server, $credentials); $SugarAPI->login(); $FilterEndpoint = $SugarAPI->filterRecords();

These Endpoint Objects manage the Request and Response to the API, as well as manage the data sent to the API. As shown above the execute() method on the Endpoint Object takes the request data that is to be sent to the Sugar API, and submits the Request the server. In the Filter Record example above, the passed in data matches what is shown in the API Endpoints documentation, however this is not always the case. The Endpoint Classes allow for manipulation of the data before being added to the Request, which means that the REST PHP Client can shorten the needed payload to allow integrated systems to quickly build requests.

One such example of this is the Bulk API Endpoint Object included in the REST PHP Client. As thedocumentation shows, the API request payload can be quite complicated, so to simplify it, it seemed intuitive to make the execute() method on the Bulk Endpoint Class, accept an array of REST PHP Client Endpoints objects, as they contain all the data needed for the Bulk request, saving the developer time by not having to build out the complex payload manually.

$SugarAPI = new \SugarAPI\SDK\SugarAPI($server,$creds); $SugarAPI->login(); $Accounts = $SugarAPI->filterRecords('Accounts') ->setData(array('max_num'=> 5)); $Contacts = $SugarAPI->filterRecords('Contacts') ->setData(array('max_num'=> 1)); $Notes = $SugarAPI->filterRecords('Notes') ->setData(array('max_num'=> 3)); $Leads = $SugarAPI->filterRecords('Leads') ->setData(array('max_num'=> 2)); $BulkCall = $SugarAPI->bulk()->execute(array( $Accounts, $Contacts, $Notes, $Leads )); $response = $BulkCall->getResponse(); if ($response->getStatus()==‘200’){ print_r($response->getBody()); }

As of right now, not all Endpoints have been added to the REST PHP Client library, however the major Endpoints used for manipulating data have been added, and more will be added in continued releases. Check out the current Endpoints that can be used here which also includes a short code snippet showcasing how to use each one.

What’s Next?

I would love to see more PHP developers using the library, which is why it is being released as an Open Source community project. Any issues that are found can be added to the GitHub repository under the Issues tab. Likewise, if you have features you would like added you can add them to the Issues tab on the repository as well. More detailed contribution guidelines can be found in the CONTRIBUTING doc , and in the Wiki.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: RESTPHPGitGitHubUTTI
分页:12
转载请注明
本文标题:Using the Sugar REST PHP Client
本站链接:http://www.codesec.net/view/484587.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(28)