PHP soapClient发送自定义XML [英] PHP soapClient send custom XML

查看:73
本文介绍了PHP soapClient发送自定义XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP的soapClient类发出SOAP请求,这是我的代码:

I'm trying to make a SOAP request using soapClient class from PHP that's my code:

$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的开头和结尾显示了一些额外的文本,如您所见:

But the request throw me an exception and when I ask the client for the last request, it shows some extra text at the begining and in the end of my XML, as you can see:

<?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?

There are some way to delete/disable that extra text? In other words, is there a way to send only my XML?

推荐答案

没有什么比提出问题来寻找答案了.我扩展了SoapClient类,并以此方式发送了正确的XML.那是代码:

There is nothing like make an question to find the answer. I extended the SoapClient class and, in this way, I send the correct XML. That's the code:

/**
 * 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来测试响应.

I use the free version of SOAP_UI to test the response.

这篇关于PHP soapClient发送自定义XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆