Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2019-09-17 14:28:11

I want to retrieve the value of a parameter from my salesforce instance. For example, I want to recover the trusted IP range: https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_securitysettings.htm

To do this, use the Metadata API. To access to this API, I use the force.com toolkit for PHP.

However, the examples given only deal with the creation or the update of the parameters:

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

How to simply get the value of the parameter (for example the trusted IP range)?

Questioner
Marius Seheia
Viewed
0
identigral 2021-04-01 05:01:27

The PHP toolkit shipped by Salesforce is quite outdated and should not be used. There are more recent forks/community projects (one,two) that attempt to implement a modern PHP client, perhaps one of these will work for you.

A simple(r) solution is to retrieve the SecuritySettings via a plain SOAP call to the Metadata API. The request payload with API version set to 46.0 should be

<?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>

and the response looks like this (only relevant portion is shown, the actual response is much larger)

<?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>

Translating to 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);

You'll need to provide an access token ($token) and a service endpoint ($serverUrl).