potagercity / salesforce-soap-client
Salesforce SOAP API 的客户端
2.0.3
2023-09-04 07:45 UTC
Requires
- php: >=7.4
- psr/log: ^1.1
- symfony/event-dispatcher: ^v6.0.2
Requires (Dev)
- phpunit/php-code-coverage: ^9.2
- phpunit/phpunit: ^9.5
README
介绍
此库是 Salesforce SOAP API 的客户端,旨在替代 Force.com Toolkit for PHP。
特性
此库的特性包括以下内容。
- PHP 和 SOAP 日期和 datetime 对象之间的自动转换。
- 将 Salesforce (UTC) 时间自动转换为您的本地时区。
- 通过事件轻松扩展:添加自定义日志记录、缓存、错误处理等。
- 通过记录迭代器,轻松遍历需要多次调用 API 的大型结果集。
- 使用 BulkSaver,通过使用批量创建、删除、更新和 Upsert 来帮助您保持在 Salesforce API 限制内。
- 使用客户端与 Symfony Mapper Bundle 一起使用,以更轻松地访问您的 Salesforce 数据。
安装
composer require php-arsenal/salesforce-soap-client
用法
客户端
使用客户端查询和操作您的组织 Salesforce 数据。首先使用构建器构建客户端
$builder = new \PhpArsenal\SoapClient\ClientBuilder( '/path/to/your/salesforce/wsdl/sandbox.enterprise.wsdl.xml', 'username', 'password', 'security_token' ); $client = $builder->build();
SOQL 查询
$results = $client->query('select Name, SystemModstamp from Account limit 5');
这将从 Salesforce 获取五个帐户,并以 RecordIterator
返回。现在您可以遍历结果。帐户的 SystemModstamp
以 \DateTime
对象的形式返回
foreach ($results as $account) { echo 'Last modified: ' . $account->SystemModstamp->format('Y-m-d H:i:') . "\n"; }
一对一关系(子查询)
子查询的结果本身也作为记录迭代器返回。因此
$accounts = $client->query( 'select Id, (select Id, Name from Contacts) from Account limit 10' ); foreach ($accounts as $account) { if (isset($account->Contacts)) { foreach ($account->Contacts as $contact) { echo sprintf("Contact %s has name %s\n", $contact->Id, $contact->Name); } } }
获取大量记录
如果您发出返回超过 2,000 条记录的查询,Salesforce API 将只返回前 2,000 条记录。使用 queryLocator
,您然后可以分批(每批 2,000 条)获取后续结果。记录迭代器会自动为您做这件事
$accounts = $client->query('Select Name from Account'); echo $accounts->count() . ' accounts returned'; foreach ($accounts as $account) { // This will iterate over the 2000 first accounts, then fetch the next 2000 // and iterate over these, etc. In the end, all your organisations’s accounts // will be iterated over. }
日志记录
要启用客户端的日志记录,请在构建器上调用 withLog()
。例如,当使用 Monolog
$log = new \Monolog\Logger('name'); $log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log')); $builder = new \PhpArsenal\SoapClient\ClientBuilder( '/path/to/your/salesforce/wsdl/sandbox.enterprise.wsdl.xml', 'username', 'password', 'security_token' ); $client = $builder->withLog($log) ->build();
现在,所有对 Salesforce API 的请求,以及它返回的响应和任何错误,都将被记录。