PHP soapClient发送自定义XML

5
我正在尝试使用PHP的soapClient类进行SOAP请求,以下是我的代码:
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://localhost:8090/mockCustomerManagement/types">
   <soapenv:Header>
      <typ:customerManagementHeader>
         <typ:userId>42</typ:userId>
         <typ:requestId>1500</typ:requestId>
         <typ:messageTimestamp>2013-09-25T14:31:21+00:00</typ:messageTimestamp>
      </typ:customerManagementHeader>
   </soapenv:Header>
   <soapenv:Body>
      <typ:getCustomerInfoData>
         <useremail>sargentoarensivia@nobody.es</useremail>
      </typ:getCustomerInfoData>
   </soapenv:Body>
</soapenv:Envelope>
XML;

$wsdl = 'http://localhost:8090/mockCustomerManagementSoapHttpBinding?WSDL';
$client = new SoapClient($wsdl, array(
    'cache_wsdl'    => WSDL_CACHE_NONE, 
    'cache_ttl'     => 86400, 
    'trace'         => true,
    'exceptions'    => true,
));

$xmlVar = new SoapVar($xmlstr, XSD_ANYXML);
$client->getCustomerInfo($xmlstr);

但是当我发送请求时,它会抛出一个异常。当我询问客户端最后一次的请求时,它显示了一些额外的文本,你可以看到它在我的 XML 开头和结尾:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost:8090/mockCustomerManagement/types">
<SOAP-ENV:Body>

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://localhost:8090/mockCustomerManagement/types">
   <soapenv:Header>
      <typ:customerManagementHeader>
         <typ:userId>42</typ:userId>
         <typ:requestId>1500</typ:requestId>
         <typ:messageTimestamp>2013-09-25T14:31:21+00:00</typ:messageTimestamp>
      </typ:customerManagementHeader>
   </soapenv:Header>
   <soapenv:Body>
      <typ:getCustomerInfoData>
         <useremail>sargentoarensivia@nobody.es</useremail>
      </typ:getCustomerInfoData>
   </soapenv:Body>
</soapenv:Envelope>

</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

有一些方法可以删除/禁用那些额外的文本吗?换句话说,是否有办法只发送我的XML?

听起来这与之前的Q&A 通过PHP SoapClient请求发送原始XML非常相似,因此进行交叉引用以供参考。为Q&A自我风格加1分。 - hakre
2个回答

8

提出问题寻找答案是最好的方法。我扩展了SoapClient类,并以此方式发送了正确的XML。以下是代码:

/**
 * Extend SoapClientClass
 */
class anotherSoapClient extends SoapClient {

    function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
        $this->server = new SoapServer($wsdl, $options);
    }
    public function __doRequest($request, $location, $action, $version) { 
        $result = parent::__doRequest($request, $location, $action, $version); 
        return $result; 
    } 
    function __anotherRequest($call, $params) {
        $location = 'http://localhost:8090/mockCustomerManagementSoapHttpBinding';
        $action = 'http://localhost:8090/mockCustomerManagementSoapHttpBinding/'.$call;
        $request = $params;
        $result =$this->__doRequest($request, $location, $action, '1');
        return $result;
    } 
}

// Create new SOAP client
$wsdl = 'http://localhost:8090/mockCustomerManagementSoapHttpBinding?WSDL';
$client = new anotherSoapClient($wsdl, array(
    'cache_wsdl'    => WSDL_CACHE_NONE, 
    'cache_ttl'     => 86400, 
    'trace'         => true,
    'exceptions'    => true,
));

// Make the request
try {
    $request = $client->__anotherRequest('getCustomerInfo', $XMLrequest);
} catch (SoapFault $e ){
    echo "Last request:<pre>" . htmlentities($client->__getLastRequest()) . "</pre>";
    exit();
}

header('Content-type: text/xml');
echo $request;

我使用SOAP_UI的免费版本来测试响应。


你也可以从XML中删除SoapEnvelope,因为它从未成为参数的一部分。 - hakre
7
请问您能够具体说明在您的示例中 $XMLrequest 是什么样子的吗? - Jacobian

-3
// xml post structure
        $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
                            <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                                  <soap:Header>
                                    <AuthHeader xmlns="http://tempuri.org/">
                                      <UserName>username</UserName>
                                      <Password>password</Password>
                                    </AuthHeader>
                                  </soap:Header>
                                  <soap:Body>
                                    <webservice_method_name xmlns="http://tempuri.org/" />
                                  </soap:Body>
                                </soap:Envelope>';   
           $headers = array(
                        "Content-type: text/xml;charset=\"utf-8\"",
                        "Accept: text/xml",
                        "Cache-Control: no-cache",
                        "Pragma: no-cache",
                        "SOAPAction:http://tempuri.org/webservice_method_name", 
                        "Content-length: ".strlen($xml_post_string),
                    );
            $url =  "http://yourwebserviceurl.com/servicename.asmx";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            $response = curl_exec($ch); 
            curl_close($ch);

//$response 显示 XML 响应

如何使用 PHP 解析 SOAP 响应,请参见以下网站

https://vaja906programmingworld.wordpress.com/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接