创建一个用于发送到SOAP服务的php数组

时间:2017-03-21 09:51:15

标签: php arrays xml soap

我有一个在SOAPUI中运行良好的hml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"
                  xmlns:wssc="http://schemas.datacontract.org/2004/07/WSSC.V4.DMS.EKV.WssDocsService">
    <soapenv:Header/>
    <soapenv:Body>
        <tem:CreateDocument>
            <!--Optional:-->
            <tem:parameters>
                <!--Optional:-->

                <!--Optional:-->
                <wssc:DocType>01</wssc:DocType>
                <!--Optional:-->

                <!--Optional:-->

                <!--Optional:-->
                <wssc:FieldValues>
                    <wssc:BaseDocumentField xsi:type="wssc:DocumentField"
                                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                        <!--Optional:-->
                        <wssc:Name>value1</wssc:Name>
                        <!--Optional:-->
                        <wssc:Value>email@email.email</wssc:Value>
                    </wssc:BaseDocumentField>
                    <wssc:BaseDocumentField xsi:type="wssc:DocumentField"
                                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                        <!--Optional:-->
                        <wssc:Name>content</wssc:Name>
                        <!--Optional:-->
                        <wssc:Value>just text</wssc:Value>
                    </wssc:BaseDocumentField>
                    <wssc:BaseDocumentField xsi:type="wssc:DocumentField"
                                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                        <!--Optional:-->
                        <wssc:Name>name</wssc:Name>
                        <!--Optional:-->
                        <wssc:Value>number</wssc:Value>
                    </wssc:BaseDocumentField>
                </wssc:FieldValues>
                <wssc:UserMail>email@email.email</wssc:UserMail>
            </tem:parameters>
        </tem:CreateDocument>
    </soapenv:Body>
</soapenv:Envelope>

我需要创建一个数组来重复其结构。 现在我有了这样一个数组,但它并没有完全发挥作用:

<?php
$params = array(
    'parameters' => array(
        'DocType' => '01',
         'UserMail' => 'email@email.email',
        'FieldValues' => array(

            'BaseDocumentField' => array(
                    'name' => 'something',
                    'value' => 'something',
                ),

                'BaseDocumentField' => array(
                    'name' => 'something',
                    'value' => 'something',
                ),

            ),
    )
);

$client = new SoapClient("http://servicename?wsdl", array("trace" => 1, "exceptions" => 0, "login" => $login, "password" => $password));
$result = $client->CreateDocument($params);
try {
        $request = $client->CreateDocument($params);
        $last_request = $client->__getLastRequest();
    } catch (SoapFault $exception) {
        $last_request = $client->__getLastRequest();
    }
var_dump($last_request);


?>

函数 __ getLastRequest 会产生以下结果:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:ns1="http://schemas.datacontract.org/2004/07/WSSC.V4.DMS.EKV.WssDocsService"
                   xmlns:ns2="http://tempuri.org/">
    <SOAP-ENV:Body>
        <ns2:CreateDocument>
            <ns2:parameters>
                <ns1:DocType>01</ns1:DocType>
                <ns1:FieldValues>
                   <ns1:BaseDocumentField/>
                </ns1:FieldValues>
                <ns1:UserMail>email@email.email</ns1:UserMail>
            </ns2:parameters>
        </ns2:CreateDocument>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我不明白如何正确构建数组以便正确显示该部分。 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

如果你正在使用复杂的WSDL,你应该明确地使用WSDL到php生成器,例如PackageGenerator项目。它将生成构造请求然后接收响应所需的所有类。使用Eclipse或PhpStorm等优秀的IDE,您将拥有完整的自动完成功能,可以轻松完成请求构建,而无需考虑如何操作。