goerik/salesforce-rest-sdk

Salesforce Rest API 的 SDK

v1.2.4 2018-12-11 15:55 UTC

README

此 API 支持以下 Salesforce API 区域

  • 限制
  • 全局描述
  • SObject 描述
  • SObject CRUD
  • SObject 获取更新/删除
  • 复合 API
    • 批量处理
    • SObject 集合
  • 批量 API
  • 流式 API

实例化 Rest 客户端

<?php

use AE\SalesforceRestSdk\Rest\Client;
use AE\SalesforceRestSdk\AuthProvider\OAuthProvider;

$client = new Client(
  new OAuthProvider(
      "SF_CLIENT_ID",
      "SF_CLIENT_SECRET",
      "https://login.salesforce.com",
      "SF_USER",
      "SF_PASS"
  )
);

如果您从 redirectUrl 返回的授权码,并希望使用它,可以这样做

<?php
use AE\SalesforceRestSdk\Rest\Client;
use AE\SalesforceRestSdk\AuthProvider\OAuthProvider;

$client = new Client(
  new OAuthProvider(
      "SF_CLIENT_ID",
      "SF_CLIENT_SECRET",
      "https://login.salesforce.com",
      null,
      null,
      OAuthProvider::GRANT_CODE,
      "https://your.redirect.uri",
      "THE_CODE_FROM_SALESFORCE"
  )
);

没有框架的 Composer 自动加载

如果您没有使用像 Symfony 这样的 PHP 框架来处理注解注册,那么您必须自己这样做

<?php

use AE\SalesforceRestSdk\Rest\Client;
use AE\SalesforceRestSdk\AuthProvider\OAuthProvider;
use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = require_once 'vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, "loadClass"));

$client = new Client(
   // ...

使用 SObject 客户端处理 SObjects

参考

<?php

//...

/** @var \AE\SalesforceRestSdk\Rest\SObject\Client $sObjectClient */
$sObjectClient = $client->getSObjectClient();

// Get basic metadata and recently used records for an object
$info = $sObjectClient->info("Account");

// Get very detailed metadata about an object
$describe = $sObjectClient->describe("Account");

// Get basic metadata info about all objects in SF
$globalDescribe = $sObjectClient->describeGlobal();

// Let's CRUD it up
$account = new \AE\SalesforceRestSdk\Model\SObject();

$sObjectClient->persist("Account", $account); // returns true if success

echo $account->Id; // outputs the SFID of the account

$account->MyCustomField__c = "Some Value I want to Save";

$sObjectClient->persist("Account", $account); // returns true on success

// Let's get new info from out account, pretend it was updated in SF
$account = $sObjectClient->get("Account", $account->Id, ["Name", "AnotherCoolField__c"]);

// Kill the account
$sObjectClient->remove("Account", $account);

// Query for more stuff
$result = $sObjectClient->query("SELECT Id, Name FROM Account");

echo $result->getTotalSize(); // OUtputs the total number of records for the query

var_dump($result->getRecords()); // SObject[]

while (!$result->isDone()) {
    // There are more records to be returned!
     // Just pass in the last result set and get the next batch
     // Lather, rinse, repeat until $result->isDone() === true;
    $result = $sObjectClient->query($result);
    
    var_dump($result->getRecords()); // CompositeSObject[]
}

// Query deleted and merged records, too
$result = $sObjectClient->queryAll("SELECT Id, Name FROM Account");

// Search for something
$result = $sObjectClient->search("FIND {Some Query} IN ALL FIELDS");

var_dump($result->getSearchRecords()); // CompositeSObject[]

实例化流式客户端

参考

<?php
use AE\SalesforceRestSdk\Bayeux\BayeuxClient;
use AE\SalesforceRestSdk\AuthProvider\OAuthProvider;
use AE\SalesforceRestSdk\Bayeux\Transport\LongPollingTransport;

$client = new BayeuxClient(
      new LongPollingTransport(),
      new OAuthProvider(
          "SF_CLIENT_ID",
          "SF_CLIENT_SECRET",
          "https://login.salesforce.com",
          "SF_USER",
          "SF_PASS"
      )
 );

订阅 PushTopic

您可以使用上面的 Rest 客户端创建一个新的 PushTopic。该主题在 Salesforce Org 中只需创建一次。所有自定义对象都由流式 API 支持,但是并非所有标准对象都支持。

支持的标准对象

  • 账户
  • 活动
  • 案例
  • 联系人
  • 合同行项目
  • 权益
  • 线索
  • 实时聊天记录
  • 机会
  • 报价
  • 报价行项目
  • 服务合同
  • 任务

使用以下方法创建或更新的任务不会在流式 API 的任务对象主题中显示。

  • 线索转换
  • 实体合并
  • 群发联系人和线索
<?php
use AE\SalesforceRestSdk\Bayeux\BayeuxClient;
use AE\SalesforceRestSdk\Bayeux\Consumer;
use AE\SalesforceRestSdk\Bayeux\ConsumerInterface;
use AE\SalesforceRestSdk\Bayeux\Message;

/** @var BayeuxClient $client */

// Getting a channel tells the client you want to subscribe to a topic
$channel = $client->getChannel('/topic/[YOUR_PUSH_TOPIC_NAME]');
// Register topic consumers prior to starting the client
$channel->subscribe(
    Consumer::create(function (ConsumerInterface $consumer, Message $message) {
        // This will be fired when the client receives a topic notification
        
        $payload = $message->getData();
        
        // The payload has information about the event that occurred
        $event = $payload->getEvent();
        
        echo $event->getType(); // "created", "updated", "undeleted", "deleted"
        echo $event->getCreatedDate()->format(\DATE_ISO8601); // outputs the datetime the event was created
        echo $event->getReplayId(); // This ia n ID used by the replay extension so it can pick up the feed where it left off
        
        $sobject = $payload->getSobject();
        
        echo $sobject->Id; // Get the Id
        echo $sobject->getFields(); // this outputs all the fields and their values that were in the create or update request
    })
);

// Start the client to begin receiving notifications;
$client->start();

$client->start(); 是一个阻塞调用,并且在此之后将不会执行任何代码,直到客户端出现错误,导致其断开连接。

例如,客户端必须在收到每个通知后 40 秒内重新连接到流式服务器。如果它无法这样做,它将尝试重新握手以创建一个新的连接。如果这失败,则客户端将断开连接,这将允许脚本的其他部分执行。

建议在单独的线程中运行流式客户端

未来添加

  • 工具 API
  • 元数据 API