PHP SoapClient:什么是NS2

时间:2016-11-29 13:50:03

标签: php xml soap soap-client soapserver

我是webservices的新手,我试图用soapClient()调用webservice,并且它正在生成不是预期格式的Request XML

以下是请求XML的预期格式

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <ns2:EndUserRequest xmlns:ns2="http://www.example.net/EndUserRequest">
        <ns2:companyCode>MD</ns2:companyCode>
        <ns2:customerBranch>60</ns2:customerBranch>
        <ns2:customerNumber>112946</ns2:customerNumber>
        <ns2:endUserName>Some Name</ns2:endUserName>
        <ns2:ContactName />
        <ns2:address />
        <ns2:city />
        <ns2:state />
        <ns2:postalCode />
        <ns2:email />
        <ns2:phoneNumber />
        <ns2:countryCode>US</ns2:countryCode>
    </ns2:EndUserRequest>
</soapenv:Body>
</soapenv:Envelope>

以下是按我的代码生成的XML请求

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.emaple.com/EndUserRequest">
    <SOAP-ENV:Body>
        <ns1:EndUserRequest xmlns:ns1="http://www.example.net/EndUserRequest">
            <companyCode>MD</companyCode>
            <customerBranchNumber>360</customerBranchNumber>
            <customerNumber>53494711</customerNumber>
            <endUserName>ABCED</endUserName>
            <ContactName></ContactName>
            <address></address>
            <city></city>
            <state></state>
            <postalCode></postalCode>
            <email></email>
            <phoneNumber></phoneNumber>
            <countryCode>US</countryCode>
        </ns1:EndUserRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

以下是我的代码

$client = new SoapClient('https://api-beta.example.com:443/enduser/v1/enduserlist?wsdl', array(
        "trace" => 1,                
        "stream_context" => stream_context_create($streamContext),
        'cache_wsdl' => WSDL_CACHE_NONE
        ));

        $endUserRequest = new stdClass;
        $endUserRequest->companyCode = 'MD';
        $endUserRequest->customerBranchNumber = '560';
        $endUserRequest->customerNumber = '59471321';
        $endUserRequest->endUserName = 'Somename';
        $endUserRequest->ContactName = '';
        $endUserRequest->address = '';
        $endUserRequest->city = '';
        $endUserRequest->state = '';
        $endUserRequest->postalCode = '';
        $endUserRequest->email = '';
        $endUserRequest->phoneNumber = '';
        $endUserRequest->countryCode = 'US';



    $requestSoapVar = new SoapVar($endUserRequest, SOAP_ENC_OBJECT,null,null,'EndUserRequest','http://www.example.com/EndUserRequest');
        $res  = $client->GetEndUsers($requestSoapVar);
        echo '<textarea style="width:600px;height:500px">';
            echo "\n-------Request Header------\n";
            echo $client->__getLastRequestHeaders();
            echo "\n-------Request------\n";
            echo $client->__getLastRequest();
            echo "\n-------Response Header------\n";
            echo $client->__getLastResponseHeaders();
            echo "\n-------Response------\n";
            echo $client->__getLastResponse();
        echo '</textarea>';

        echo '<textarea style="width:600px;height:500px">';
        print_r($res);
        echo '</textarea>';

1 个答案:

答案 0 :(得分:3)

对象的每个成员都必须是SoapVar对象,因为它们有一个名称空间。只需编码您的对象,如下面的示例所示。

$oEndUserRequest = new StdClass();
$oEndUserRequest->companyCode = new SoapVar(
    'MD', 
    XSD_STRING, 
    null, 
    null,
    'companyCode',
    'http://www.example.com/EndUserRequest'
);

只为everey班级成员做,你就会得到预期的结果。

出于高级原因,这里有一个如何更改命名空间前缀的示例。您必须知道,PHP SoapClient对象和SoapVar对象都有办法手动设置名称空间前缀。在正常情况下,不必为命名空间设置前缀。

PHP SoapClient对象有一个__doRequest方法,您可以在其中编辑XML。您必须编写自己的SoapClient代码来扩展PHP SoapClient。

class MySoapClient extends SoapClient {
    public function __doRequest($sRequest, $sLocation, $sAction, $iVersion, $iOneWay = 0) {
        $sRequest = str_replace('ns1', 'ns2', $sRequest);
        $this->__last_request = $sRequest;

        return parent::__doRequest(ltrim($sRequest), $sLocation, $sAction, $iVersion, $iOneWay);
    }
}

在我看来,不需要更改名称空间前缀。如果是这样,只需使用__doRequest方法即可。