inetprocess/sugarcrm-apiwrapper

为 SugarCRM 7 设计的一个非常简单的 API 包装器

v1.0.10 2021-09-01 08:36 UTC

This package is not auto-updated.

Last update: 2024-09-12 00:05:12 UTC


README

安装

composer require inetprocess/sugarcrm-apiwrapper

使用方法

BaseRequest

一个基本的类,用于启动 Guzzle HTTP 客户端,并为其他类提供 SugarCRM API 调用的主要方法。您可以直接使用它,但并不推荐。您也可以基于它构建自己的类。

示例用法

<?php

require_once 'vendor/autoload.php';

use InetProcess\SugarAPI\BaseRequest;

$url = 'http://127.0.0.1';
$username = 'admin';
$password = 'admin';

$base = new BaseRequest($url);
$base->setUsername($username)->setPassword($password);

// The login can be called dynamically as the class will detect you are not logged
// But to save an API Request, call it manually
$base->login();

// Get the list of Contacts
// '200' is the expected Status Code. If it's not the right one, you'll get an Exception
$data = $base->request('/Contacts', ['OAuth-Token' => $base->getToken()], [], 'get', 200);

其他有用的方法

以下方法允许您对 SugarAPIWrapper 有更多的控制

  • getBaseUrl() : 返回 BaseURL
  • getClient() : 返回 GuzzleClient
  • getToken() : 获取 SugarCRM 发送的 token
  • getTokenExpiration() : 获取 Sugar 发送的 Token 过期时间
  • setToken(string $token)setTokenExpiration(\DateTime $date) : 必须一起使用,以避免登录
  • setLogger(\Psr\Log\LoggerInterface $logger) 设置 PSR 日志记录器
  • setPlatform(string $platform) : 定义平台(默认为 inetprocess

SugarClient

BaseRequest 的扩展,提供 GET、POST、PUT 和 DELETE 的包装器。它会自动登录并发送带有 token 的正确头信息。

初始化客户端

<?php

require_once 'vendor/autoload.php';

use InetProcess\SugarAPI\SugarClient;

$url = 'http://127.0.0.1';
$username = 'admin';
$password = 'admin';

$client = new SugarClient($url);
$client->setUsername($username)->setPassword($password);

向任何端点 POST

<?php
$data = ['first_name' => 'Emmanuel', 'last_name' => 'D.'];
$contact = $client->post('/Contacts', $data);

echo $contact['last_name']; // Should display: "D."

向任何端点 PUT

在执行 POST 后,执行以下操作

<?php
$data = ['first_name' => 'Emmanuel', 'last_name' => 'Dy.'];
$contact = $client->put('/Contacts/' . $contact['id'], $data);

echo $contact['last_name']; // Should display: "Dy"

向任何端点 GET

<?php
$contact = $client->get('/Contacts/' . $contact['id']);

echo $contact['last_name']; // Should display: "D."

向任何端点 DELETE

<?php
$contact = $client->delete('/Contacts/' . $contact['id']);

使用批量请求在单个 HTTP 调用中发送多个请求

获取新的 BulkRequest 对象

<?php
$bulk = $client->newBulkRequest();

发送多个请求

您可以使用与 SugarClient 类相同的函数来准备您的请求

<?php
$bulk->post('/Contacts', $data);
$bulk->delete('/Contacts/'.$contact['id']);
...
$responses = $bulk->send();

模块

为特定模块操作提供包装器。它会自动登录并发送带有 token 的正确头信息。

初始化客户端和模块类

<?php

require_once 'vendor/autoload.php';

use InetProcess\SugarAPI\Module;
use InetProcess\SugarAPI\SugarClient;

$url = 'http://127.0.0.1';
$username = 'admin';
$password = 'admin';

$client = new SugarClient($url);
$client->setUsername($username)->setPassword($password);

$module = new Module($client);

记录计数

通过应用筛选器计数记录

  • $module : 模块名称,例如 联系人
  • $filters : 筛选器数组,如 SugarCRM 文档 中定义

使用 name = Test 的示例笔记计数

<?php
$numNotes = $module->search('Notes', [['name' => 'Test']]);

echo "$numNotes in SugarCRM with name = Test";

搜索记录

通过应用筛选器搜索记录(筛选器的结构与计数相同)。

  • $module : 模块名称,例如 联系人
  • $filters : 筛选器数组,如 SugarCRM 文档 中定义
  • $fields : 要获取的字段列表,默认为全部
  • $offset : 默认为 0
  • $maxNum : 默认为 20
  • $orderBy : 默认为 null

示例搜索,应检索最多 10 个带有 name = Test 的笔记,并按名称排序

<?php
$notes = $module->search('Notes', [['name' => 'Test']], [], 0, 10, 'name');

if (!empty($notes['records'])) {
    echo $notes['records'][0]['name']; // Displays 'Test'
}

检索一个或多个记录

  • $module : 模块名称,例如 联系人
  • $record : 记录 ID,默认为 null
  • $offset : 默认为 0
  • $maxNum : 默认为 20

检索最多 10 条记录

<?php
$notes = $module->retrieve('Notes', null, 0, 10);

if (!empty($notes['records'])) {
    echo $notes['records'][0]['name']; // Displays the name of the note
}

检索单个记录(如果不存在,则抛出 SugarAPIException 异常)

<?php
$noteId = '123456-abcdef-78910';
$note = $module->retrieve('Notes', $noteId);

if (!empty($note)) {
    echo $note['name']; // Displays the name of the note
}

创建记录

参数

  • $module : 模块名称,例如 联系人
  • $data : 字段 => 值的数组
<?php
$data = $module->create('Notes', ['name' => 'Name']);

echo $data['note']; // Displays New Name

更新记录

参数

  • $module : 模块名称,例如 联系人
  • $record : 记录 ID
  • $data : 字段 => 值的数组
<?php

$noteId = '123456-abcdef-78910';
$data = $module->update('Notes', $noteId, ['name' => 'New Name']);

echo $data['note']; // Displays New Name

删除记录

参数

  • $module : 模块名称,例如 联系人
  • $record : 记录 ID
<?php

$noteId = '123456-abcdef-78910';
$module->delete('Notes', $noteId);

设置记录的所有相关 ID

参数

  • $module : 模块名称,例如 联系人
  • $record : 记录 ID
  • $linkName : 关系名称
  • $relatedIds: 来自相关模块的 ID 数组。此数组是相关 ID 的完整集合,如果未在此处发送,则将删除现有相关链接。
<?php
$contactId = '123456-abcdef-78910';
$casesIds = ['756335-abcdef-12340', '5475626-fedba-545761'];
$res = $module->updateRelatedLinks('Contacts', $contactId, 'cases', $casesIds);
var_export($res);
/*  [
        'linked_records' => ['756335-abcdef-12340', '5475626-fedba-545761'],
        'unlinked_records' => ['234782-gfbeaf-7672'],
        'errors' => [],
    ]
*/

下载文件

参数

  • $module : 模块名称,例如 联系人
  • $record : 记录 ID
  • $field : 字段名称
  • $targetFile : 要写入内容的本地文件。 如果为空,则直接返回数据
  • $originalName : 在 Sugar 中显示的文件名称

以下示例下载文件并将其内容放入 $targetFile 文件中

<?php
$noteId = '123456-abcdef-78910';
$targetFile = '/tmp/file123456';
$module->download('Notes', $noteId, 'filename', $targetFile);

上传文件

参数

  • $module : 模块名称,例如 联系人
  • $record : 记录 ID
  • $field : 字段名称
  • $filePath : 本地文件路径
  • $originalName : 在 Sugar 中显示的文件名称
<?php

$noteId = '123456-abcdef-78910';
$localFile = '/tmp/file123456';
$uploadedFile = $module->upload('Notes', $noteId, 'filename', $localFile, 'My File.txt');

echo $uploadedFile['filename']['name']; // Displays "My File.txt"

下拉菜单

检索下拉菜单的键/值,使用当前用户(在setUserName中定义的用户)的语言。

参数

  • $module : 模块名称,例如 联系人
  • $field : 需要获取值的字段
<?php

require_once 'vendor/autoload.php';

use InetProcess\SugarAPI\Dropdown;
use InetProcess\SugarAPI\SugarClient;

$url = 'http://127.0.0.1';
$username = 'admin';
$password = 'admin';

$client = new SugarClient($url);
$client->setUsername($username)->setPassword($password);

$dropdown = new Dropdown($client);
$salesStages = $dropdown->getDropdown('Opportunities', 'sales_stage');