如何获取php响应中的xml

我正在尝试使用以下代码从服务器获取响应:


<?php


$url = "http://pgtest.redserfinsa.com:2027/WebPubTransactor/TransactorWS?WSDL";


$post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:web="http://webservices.serfinsa.sysdots.com/">

   <soapenv:Header/>

   <soapenv:Body>

      <web:cardtransaction>

         <!--Optional:-->

         <security>{"comid":"comid","key":"$!@!@!@!@!@","comwrkstation":"comwrkstation"}</security>

         <!--Optional:-->

         <txn>MAN</txn>

         <!--Optional:-->

         <message>{"CLIENT":"9999994570392223"}

            </message>

      </web:cardtransaction>

   </soapenv:Body>

</soapenv:Envelope>';



$post_data = array('xml' => $post_string);

$stream_options = array(

    'http' => array(

        'method'  => 'POST',

        'header'  => 'Content-type: text/xml' . "\r\n",

        'content' =>  http_build_query($post_data)));



$context  = stream_context_create($stream_options);

$response = file_get_contents($url, null, $context);

?>

但是,我仍然收到空回复,有人知道吗?


茅侃侃
浏览 130回答 1
1回答

HUX布斯

我不知道为什么你的代码返回空,但你可以尝试使用curl代码:$url = "http://pgtest.redserfinsa.com:2027/WebPubTransactor/TransactorWS?WSDL";$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:web="http://webservices.serfinsa.sysdots.com/">&nbsp; &nbsp;<soapenv:Header/>&nbsp; &nbsp;<soapenv:Body>&nbsp; &nbsp; &nbsp; <web:cardtransaction>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<security>{"comid":"comid","key":"$!@!@!@!@!@","comwrkstation":"comwrkstation"}</security>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<txn>MAN</txn>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<message>{"CLIENT":"9999994570392223"}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</message>&nbsp; &nbsp; &nbsp; </web:cardtransaction>&nbsp; &nbsp;</soapenv:Body></soapenv:Envelope>';$headers = array(&nbsp; "Content-type: text/xml",&nbsp; "Content-length: " . strlen($xml),&nbsp; "Connection: close",);$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_TIMEOUT, 30);curl_setopt($curl, CURLOPT_POST, true);curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);$response = curl_exec($curl);$error = curl_error($curl);print_r($response);print_r($error);结果:接收失败:连接被对等方重置
打开App,查看更多内容
随时随地看视频慕课网APP