danone89/restforcephp

PHP Salesforce REST

2.1.8 2023-03-03 11:34 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:09:28 UTC


README

Travis Downloads Packagist Code Climate Test Coverage

此项目旨在模拟 ejhomes/restforce gem 为 rails 所做的操作。

安装

此库需要 PHP 7.1 或更高版本;我们建议使用最新可用的 PHP 版本。

$ composer require danone89/restforcephp

或者。

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

{
    "require": {
        "danone89/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/