我是webservice s新手,我正试图用soapclient()调用webservice,它正在生成不符合预期格式的请求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>';

最佳答案

对象的每个成员都必须是soapvar对象,因为它们有一个命名空间。只需按照下面的示例对对象进行编码。

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

只要为每个班级成员做这件事,你就会得到预期的结果。
出于高级原因,下面是一个如何更改名称空间前缀的示例。您必须知道,php soapclient对象和soapvar对象都可以手动设置名称空间前缀。在正常情况下,不必为命名空间设置前缀。
PHP SOAPCurror对象有一个Y-OrdReQuestMy方法,您可以在其中编辑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方法。

10-07 21:03