使用PHP SOAPClient获取XML响应

时间:2015-08-26 14:44:56

标签: php soap

我正在尝试使用php本机SoapClient函数复制此结构

$client = new SoapClient('my.wsdl', ['trace'=>1, 'cache_wsdl' => WSDL_CACHE_NONE]);
$client->MethodToCall($params);
var_dump($client->__getLastRequest());

这是预期的xml请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
        <MethodToCall> 
            <MethodToCallRQ> 
                <RqHeader HostID="123456" Xtoken="TOKEN123456" Interface="WEB" UserName="WEB"/> 
                <SearchType>NAME</SearchType> 
                <RecordCode/> 
                <Name>MARIO</Name> 
            </MethodToCallRQ> 
        </MethodToCall>
    </soapenv:Body> 
</soapenv:Envelope>

目前我无法复制它。 我需要一些提示,以正确的方式将参数传递给我的方法。

UPGRADE。

我使用wsdl2phpgenerator为我的请求创建了所有类。

我的代码结果如下:

$service = new \SoapExtended(
    [
        'soap_version'   => SOAP_1_1,
        'exceptions'     => true,
        'trace'          => 1,
        'location'       => MYLOCATION,
        'cache_wsdl'     => WSDL_CACHE_NONE,
    ],
    MYWSDL
);

$params = new \MethodToCallRQ("NAME");
$params->setName("MARIO");
$params->setRqHeader(new \RequestHeader("123456", "TOKEN123456", "WEB", "WEB"));

$request = new \MethodToCall($params);
$response = $service->MethodToCall($request); 
// above call is equal to this below:
//$this->__soapCall('MethodToCall', array($request));

$ request的转储是:

object MethodToCall (1) {
    protected MethodToCallRQ -> object MethodToCallRQ (10) {
        protected SearchType -> string (4) "NAME"
        protected RecordCode -> NULL
        protected Name -> string (7) "Stefano"
        protected Surname -> NULL
        protected VatCode -> NULL
        protected ZipCode -> NULL
        protected City -> NULL
        protected CountyCode -> NULL
        protected CategoryCode -> NULL
        protected RqHeader -> object RequestHeader (5) {
            protected HostID -> string (6) "123456"
            protected Xtoken -> string (11) "TOKEN123456"
            protected Interface -> string (3) "WEB"
            protected UserName -> string (3) "WEB"
        }
    }
}

生成的xml是:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <parameters>
            <MethodToCallRQ>
                <SearchType>NAME</SearchType>
                <RecordCode/>
                <Name>Stefano</Name>
                <Surname/>
                <VatCode/>
                <ZipCode/>
                <City/>
                <CountyCode/>
                <CategoryCode/>
                <RqHeader>
                    <HostID>123456</HostID>
                    <Xtoken>TOKEN123456</Xtoken>
                    <Interface>WEB</Interface>
                    <UserName>WEB</UserName>
                </RqHeader>
            </MethodToCallRQ>
        </parameters>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

为什么要标记

<parameters> 

而不是

<MethodToCall> 

1 个答案:

答案 0 :(得分:0)

使用WSDL可能有助于查看预期的请求以及它无法正常工作的原因。

您尝试使用PackageGenerator了吗?那么让我知道