eventfarm/restforcephp

PHP Salesforce REST

v2.3.1 2024-01-03 06:06 UTC

README

Travis Downloads Packagist

此库旨在模拟 ejhomes/restforce gem 在 rails 中的行为。

安装

此库需要 PHP 7.1 或更高版本;我们建议使用最新的 PHP 版本。它已经通过最新的 PHP 版本进行了测试(写作时为 v8.3)

$ composer require eventfarm/restforcephp

或者。

将以下行添加到您的 composer.json 文件中。

{
    "require": {
        "eventfarm/restforcephp": "^2.0.0"
    }
}
$ composer install

项目默认值

<?php
namespace App;

use EventFarm\Restforce\Rest\OAuthAccessToken;
use EventFarm\Restforce\Restforce;
use EventFarm\Restforce\RestforceInterface;

class DemoSalesforceApi
{
    /** @var null|RestforceInterface $restforce */
    private $restforce;

    public function getRestforceClient(): RestforceInterface
    {
        if ($this->restforce === null) {
            // You need either the OAuthAccessToken
            // or the Username & Password,
            // the other(s) can be null.
            $this->restforce = new Restforce(
                getenv('SF_CLIENT_ID'),
                getenv('SF_CLIENT_SECRET'),
                new OAuthAccessToken(...),
                getenv('SF_USERNAME'),
                getenv('SF_PASSWORD')
            );
        }
        return $this->restforce;
    }
}

访问令牌信息

OAuth 范围

请参阅 Salesforce OAuth 2.0 文档 了解您的应用程序需要哪些可用的 OAuth 范围。

销售force 文档

每个部分的 Salesforce 文档页面链接。或者,这里是 Salesforce 端点的圣杯。

用法

限制

文档 返回 Salesforce API 的每日 API 限制列表。有关选项的完整列表,请参阅文档。

public function limits(): \Psr\Http\Message\ResponseInterface

<?php
/** @var \EventFarm\Restforce\RestforceInterface $restforce */
$restforce = (new DemoSalesforceApi())->getClient();
/** @var \Psr\Http\Message\ResponseInterface $responseInterface */
$responseInterface = $restforce->limits();

用户信息

文档 获取登录用户的信息。

public function limits(): \Psr\Http\Message\ResponseInterface

<?php
/** @var \EventFarm\Restforce\RestforceInterface $restforce */
$restforce = (new DemoSalesforceApi())->getClient();
/** @var \Psr\Http\Message\ResponseInterface $responseInterface */
$responseInterface = $restforce->userInfo();

查询

文档 使用查询资源执行 SOQL 查询,该查询在一个单独的响应中返回所有结果。

public function query(string $query): \Psr\Http\Message\ResponseInterface

<?php
/** @var \EventFarm\Restforce\RestforceInterface $restforce */
$restforce = (new DemoSalesforceApi())->getClient();
/** @var \Psr\Http\Message\ResponseInterface $responseInterface */
$responseInterface = $restforce->query('SELECT Id, Name FROM Account');

查找

文档 查找资源 $id$sobject,可选地指定在字段参数中要检索的字段,并使用资源的 GET 方法。

public function find(string $sobject, string $id, array $fields = []): \Psr\Http\Message\ResponseInterface

<?php
/** @var \EventFarm\Restforce\RestforceInterface $restforce */
$restforce = (new DemoSalesforceApi())->getClient();
/** @var \Psr\Http\Message\ResponseInterface $responseInterface */
$responseInterface= $restforce->find('Account', '001410000056Kf0AAE');

描述

文档 完全描述了指定对象的各个级别的单个元数据。

public function describe(string $sobject): \Psr\Http\Message\ResponseInterface

<?php
/** @var \EventFarm\Restforce\RestforceInterface $restforce */
$restforce = (new DemoSalesforceApi())->getClient();
/** @var \Psr\Http\Message\ResponseInterface $responseInterface */
$responseInterface = $restforce->describe('Account');

创建

文档 创建 $sobject 的新记录。如果调用成功,响应体将包含创建记录的 ID。

public function create(string $sobject, array $data): \Psr\Http\Message\ResponseInterface

<?php
/** @var \EventFarm\Restforce\RestforceInterface $restforce */
$restforce = (new DemoSalesforceApi())->getClient();
/** @var \Psr\Http\Message\ResponseInterface $responseInterface */
$responseInterface = $restforce->create('Account', [
    'Name' => 'Foo Bar'
]);

更新

文档 您使用 SObject Rows 资源来更新记录。响应将是 $success 的布尔值。

public function update(string $sobject, string $id, array $data):bool

<?php
/** @var \EventFarm\Restforce\RestforceInterface $restforce */
$restforce = (new DemoSalesforceApi())->getClient();
/** @var \Psr\Http\Message\ResponseInterface $responseInterface */
$responseInterface = $restforce->update('Account', '001i000001ysdBGAAY', [
    'Name' => 'Foo Bar Two'
]);

贡献

感谢您考虑为我们的 Restforcephp 项目做出贡献。只需注意以下几点

  • 确保您的提交符合 PSR-2 编码标准。
  • 确保您的提交信息定义良好。
  • 确保您已为您的更改添加了必要的单元测试。
  • 运行所有测试以确保没有其他东西被意外破坏。
  • 提交一个拉取请求。

单元测试

$ vendor/bin/phpunit
带有代码覆盖率
$ vendor/bin/phpunit --coverage-text --coverage-html coverage_report

检查PHP-CS PSR2测试

$ vendor/bin/phpcs -p --standard=PSR2 src/ tests/

应用PHP-CS PSR2修复

自动运行并解决一些低垂的PSR2修复,这可能不会全部解决,所以之后请重新运行检查。

$ vendor/bin/phpcbf --standard=PSR2 src/ tests/

检查PHP版本的兼容性

$ vendor/bin/phpcs -p ./src/ --standard=PHPCompatibility --runtime-set testVersion 7.1-8.4