协助使用 PHP Soap 请求属性和节点


Assistance with PHP Soap request with attributes and nodes

编辑,请参阅我的最后一条评论。我注释了将 fullName 添加到元数据数组的代码。

我在使用 PHP Soap 客户端创建以下 XML Soap 请求时遇到问题。我遇到的问题是将 fullName 元素作为子节点添加到 sObjects。

<?xml version="1.0" encoding="UTF-8"?>
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn1="urn:tooling.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<x:Header>
    <urn1:SessionHeader>
        <urn1:sessionId>a sessionid</urn1:sessionId>
    </urn1:SessionHeader>
    <urn1:MetadataWarningsHeader>
        <urn1:ignoreSaveWarnings>false</urn1:ignoreSaveWarnings>
    </urn1:MetadataWarningsHeader>
    <urn1:AllOrNoneHeader>
        <urn1:allOrNone>false</urn1:allOrNone>
    </urn1:AllOrNoneHeader>
    <urn1:CallOptions>
        <urn1:client/>
    </urn1:CallOptions>
</x:Header>
<x:Body>
    <urn1:create>
        <urn1:sObjects xsi:type="WorkflowFieldUpdate">
            <fullName>Lead.Tooling</fullName>
            <metadata>
                <field>Test_Field_Update__c</field>
                <name>Tooling</name>
                <notifyAssignee>false</notifyAssignee>
                <operation>Null</operation>
                <protected>false</protected>
            </metadata>
        </urn1:sObjects>
    </urn1:create>
</x:Body>

这是我在创建函数中使用的代码。我得到了一切,但全名。我尝试添加它作为带有元数据节点的数组的一部分,但它入为虚假的。

$field = new SoapVar('Test_Field_Update__c', XSD_STRING, null, null, 'field', null);
$name = new SoapVar('ToolingSample', XSD_STRING, null, null, 'name', null);
$notifyAssignee = new SoapVar(false, XSD_BOOLEAN, null, null, 'notifyAssignee');
$protected = new SoapVar(false, XSD_BOOLEAN, null, null, 'protected');
$operation = new SoapVar('Null', XSD_STRING, null, null, 'operation');
$fullname = new SoapVar('Lead.ToolingSample', XSD_STRING, null, null, 'fullName', null);
$metadata = new SoapVar(
    array($field, $name, $notifyAssignee, $protected, $operation),
    SOAP_ENC_OBJECT,
    null,
    null,
    'Metadata'
);
// I needed change this line SoapVar
$metadataList = new SoapVar(array($metadata), SOAP_ENC_OBJECT);
// Fix: Is adding the fullName var to the $metadata array
$metadataList = SoapVar(array($metadata, $fullname), SOAP_ENC_OBJECT);
$sObject = new SoapVar($metadataList, SOAP_ENC_OBJECT, 'WorkflowFieldUpdate', null, 'sObjects');
$sObjects = new SoapVar(array($sObject), SOAP_ENC_OBJECT, null, null, null);
$response = $soapClient->create(new SoapVar($sObjects, SOAP_ENC_OBJECT));
$ReturnArray = array('success' => true, 'data' => $response, 'types' => $metadataList, 'request' => $soapClient->__getLastRequest());
echo $_REQUEST['callback'] . '(' . json_encode($ReturnArray) . ')';

我尝试将sObject Soapvar更改为此,但没有去。我不太相信它会起作用。

$sObject = new SoapVar(array($fullName, $metadataList), SOAP_ENC_OBJECT, 'WorkflowFieldUpdate', null, 'sObjects')

正确的完整代码如下。请参阅代码中的注释以进行修复。稍后我将为 soap 客户端和不同的 sObject 添加一个类。

<?php
ini_set('display_errors', true);
ini_set("soap.wsdl_cache_enabled", "0");
error_reporting(-1);
$ReturnArray = null;
try {
    if (isset($_REQUEST['sessionId']) && isset($_REQUEST['serverUrl'])) {
        $sessionId = $_REQUEST['sessionId'];
        $serverUrl = str_replace('/u/', '/t/',$_REQUEST['serverUrl']);
        $namespace = 'urn:tooling.soap.sforce.com';
        $soapClientArray = array(
            'user_agent' => 'salesforce-toolkit-php/35.0',
            'encoding' => 'utf-8',
            'trace' => 1,
            'sessionId' => $sessionId
        );
        $sessionVar = array(
            'sessionId' => new SoapVar($sessionId, XSD_STRING)
        );
        $headerBody = new SoapVar(
            $sessionVar,
            SOAP_ENC_OBJECT
        );
        $session_header = new SoapHeader(
            $namespace,
            'SessionHeader',
            $headerBody,
            false
        );
        $soapClient = new SoapClient('tooling.wsdl.xml', $soapClientArray);
        $header_array    = array(
            $session_header
        );
        $soapClient->__setSoapHeaders($header_array);
        $field = new SoapVar('Test_Field_Update__c', XSD_STRING, null, null,'field', null);
        $name = new SoapVar('ToolingSample', XSD_STRING, null, null, 'name', null);
        $notifyAssignee = new SoapVar(false, XSD_BOOLEAN, null, null, 'notifyAssignee');
        $protected = new SoapVar(false, XSD_BOOLEAN, null, null, 'protected');
        $operation = new SoapVar('Null', XSD_STRING, null, null, 'operation');
        $fullname = new SoapVar('Lead.ToolingSample', XSD_STRING, null, null, 'fullName', null);
        $metadata = new SoapVar(
            array(
                $field,
                $name, 
                $notifyAssignee,  
                $protected, $operation
            ),
            SOAP_ENC_OBJECT,
            null,
            null,
           'Metadata'
        );
        // I needed change this line SoapVar
        // $metadataList = new SoapVar(array($metadata), SOAP_ENC_OBJECT);
        // Fix: Is adding the fullName var to the $metadata array
        $metadataList = SoapVar(
            array($metadata, $fullname),
            SOAP_ENC_OBJECT
        );
        $sObject = new SoapVar(
            $metadataList,
            SOAP_ENC_OBJECT,
            'WorkflowFieldUpdate',
            null,
            'sObjects'
        );
        $sObjects = new SoapVar(
            array($sObject),
            SOAP_ENC_OBJECT,
            null,
            null,
            null
        );
        $response = $soapClient->create(new SoapVar($sObjects, SOAP_ENC_OBJECT));
        // You can add anything to this array and it will post
        $ReturnArray = array(
            'success' => true,
            'data' => $response,
        );
    } else {
        $ReturnArray = array(
            'success'  => false,
            'data' => 'Unknown error'
        );
    }
} catch (Exception $e) {
    $ReturnArray = array(
        'success'  => false,
        'data' => $e->getMessage()
    );
}
// This will output code in the console where it's easier to interpret
echo $_REQUEST['callback'] . '(' . json_encode($ReturnArray) . ')';