如何使用 force.com 工具包为 PHP 从 salesforce 检索元数据?

How to retrieve metadata from salesforce with force.com toolkit for PHP?

我想从我的 salesforce 实例中检索参数值。例如,我想恢复受信任的 IP 范围: https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_securitysettings.htm

为此,请使用元数据 API。要访问此 API,我使用 force.com 工具包 PHP。

但是,给出的示例仅涉及参数的创建或更新:

https://developer.salesforce.com/blogs/developer-relations/2011/11/extending-the-force-com-toolkit-for-php-to-handle-the-metadata-api.html

如何简单获取参数的值(例如可信IP范围)?

Salesforce 提供的 PHP 工具包已经过时,不应使用。最近有更多 forks/community 项目 (one,two) 试图实现现代 PHP 客户端,也许其中之一适合您。

一个简单的(r) 解决方案是通过对元数据的普通 SOAP 调用来检索 SecuritySettings API。 API 版本设置为 46.0request payload 应该是

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>Security</members>
        <name>Settings</name>
    </types>
    <version>46.0</version>
</Package>

响应如下所示(仅显示相关部分,实际响应要大得多

<?xml version="1.0" encoding="UTF-8"?>
<SecuritySettings xmlns="http://soap.sforce.com/2006/04/metadata">
    <networkAccess>
        <ipRanges>
            <description>...</description>
            <end>1.255.255.255</end>
            <start>0.0.0.0</start>
        </ipRanges>
    </networkAccess>
</SecuritySettings>

翻译成PHP:

$wsdl = PUBLIC_PATH . '/wsdl-metadata.xml'; 
$apiVersion = 46.0;
$singlePackage = true;
$members = 'Security';
$name = 'Settings';

$params = new StdClass();
$params->retrieveRequest = new StdClass();
$params->retrieveRequest->apiVersion = $apiVersion;
$params->retrieveRequest->singlePackage = $singlePackage;
$params->retrieveRequest->unpackaged = new StdClass();
$params->retrieveRequest->unpackaged->version = $apiVersion;
$params->retrieveRequest->unpackaged->type = new stdClass();
$params->retrieveRequest->unpackaged->type->members = $members;
$params->retrieveRequest->unpackaged->type->name = $name;

$option = [
    'trace' => TRUE,
];
// Namespaces
$namespace = 'http://soap.sforce.com/2006/04/metadata';
$client = new SoapClient($wsdl, $option);

$header = new SoapHeader($namespace, "SessionHeader", array ('sessionId' => $token)); // NEED: access token
$client->__setSoapHeaders($header);
$client->__setLocation($serverUrl); // NEED: service endpoint URL
$serviceResult = $client->retrieve($params);

您需要提供访问令牌 ($token) 和服务端点 ($serverUrl)。

对于任何试图让它工作的人,identigral 的示例对我不起作用,必须执行以下操作:

$client->setEndpoint($serverUrl);更改为$client->__setLocation($serverUrl);

我使用 Oauth 登录,所以您需要从响应中构建 $serverUrl: + '/services/Soap/m/46.0/' +

示例: 'https://your-production-or-sandbox-name.my.salesforce.com/services/Soap/m/46.0/your-org-id'