phpforce/soap-client

用于 Salesforce SOAP API 的 PHP 客户端

0.1.0 2015-04-05 15:19 UTC

This package is auto-updated.

Last update: 2024-09-06 08:31:00 UTC


README

Build Status
Scrutinizer Code Quality

###我在寻找维护者!

PHPForce Soap 客户端:用于 Salesforce SOAP API 的 PHP 客户端

简介

此库是 Salesforce SOAP API 的客户端,旨在替代 Force.com Tookit for PHP

功能

此库的功能包括以下内容。

  • PHP 和 SOAP 日期和 datetime 对象之间的自动转换。
  • 将 Salesforce (UTC) 时间自动转换为您的本地时区。
  • 通过事件轻松扩展:添加自定义日志记录、缓存、错误处理等。
  • 通过记录迭代器轻松遍历需要多次调用 API 的大型结果集。
  • BulkSaver 通过使用批量创建、删除、更新和合并来帮助您保持在 Salesforce API 的限制内。
  • 完全单元测试(仍在进行中)。
  • 使用客户端与 Symfony2 Mapper Bundle 结合使用,以更轻松地访问 Salesforce 数据。

安装

此库可在 Packagist 上找到。安装此库的推荐方法是通过 Composer

$ php composer.phar require phpforce/soap-client dev-master

用法

客户端

使用客户端查询和操作您的组织 Salesforce 数据。首先使用构建器构建客户端

$builder = new \Phpforce\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);
        }
    }
}

获取大量记录

如果您发出返回超过 2000 条记录的查询,Salesforce API 只会返回前 2000 条记录。使用 queryLocator,您然后可以以每批 2000 条记录的方式获取后续结果。记录迭代器会自动为您执行此操作

$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 \Phpforce\SoapClient\ClientBuilder(
  '/path/to/your/salesforce/wsdl/sandbox.enterprise.wsdl.xml',
  'username',
  'password',
  'security_token'
);
$client = $builder->withLog($log)
  ->build();

现在将记录所有对 Salesforce API 的请求,以及它返回的响应和任何错误。