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

时间:2016-01-14 17:41:26

标签: php xml soap xml-parsing salesforce

编辑,查看我的上一条评论。我评论了将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>

以下是我在create function中使用的代码。我得到了一切,但是全名。我尝试使用Metadata节点添加is作为数组的一部分,但它作为Bogus插入。

$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')

1 个答案:

答案 0 :(得分:0)

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

<?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) . ')';