如何过滤来自CURL HTTP POST请求的XML响应

时间:2018-03-14 17:26:22

标签: php xml curl soap

来自CURL请求的回复:

<?xml version="1.0" encoding="UTF-8"?>
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
   <soap-env:header>
      <soap-env:body>
         <ipgapi:ipgapiorderresponse xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi" xmlns:a1="http://ipg-online.com/ipgapi/schemas/a1" xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1">
            <ipgapi:approvalcode>Y:761862:4515799310:PPXP:0037</ipgapi:approvalcode>
            <ipgapi:avsresponse>PPX</ipgapi:avsresponse>
            <ipgapi:brand>VISA</ipgapi:brand>
            <ipgapi:country>GBR</ipgapi:country>
            <ipgapi:commercialserviceprovider>BOSMS</ipgapi:commercialserviceprovider>
            <ipgapi:orderid>A-966025d3-81a2-453a-820e-bb145e8390d1</ipgapi:orderid>
            <ipgapi:ipgtransactionid>84515799310</ipgapi:ipgtransactionid>
            <ipgapi:paymenttype>CREDITCARD</ipgapi:paymenttype>
            <ipgapi:processorapprovalcode>761862</ipgapi:processorapprovalcode>
            <ipgapi:processorccvresponse>P</ipgapi:processorccvresponse>
            <ipgapi:processorreferencenumber>761862</ipgapi:processorreferencenumber>
            <ipgapi:processorresponsecode>00</ipgapi:processorresponsecode>
            <ipgapi:processorresponsemessage>AUTH CODE:761862</ipgapi:processorresponsemessage>
            <ipgapi:tdate>1521047872</ipgapi:tdate>
            <ipgapi:tdateformatted>2018.03.14 18:17:52 (CET)</ipgapi:tdateformatted>
            <ipgapi:terminalid>21400371</ipgapi:terminalid>
            <ipgapi:transactionresult>APPROVED</ipgapi:transactionresult>
            <ipgapi:transactiontime>1521047872</ipgapi:transactiontime>
         </ipgapi:ipgapiorderresponse>
      </soap-env:body>
   </soap-env:header>
</soap-env:envelope>

我尝试了以下内容:

$responseXML = '<?xml version="1.0" encoding="UTF-8"?>
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
   <soap-env:header>
      <soap-env:body>
         <ipgapi:ipgapiorderresponse xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi" xmlns:a1="http://ipg-online.com/ipgapi/schemas/a1" xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1">
            <ipgapi:approvalcode>Y:761862:4515799310:PPXP:0037</ipgapi:approvalcode>
            <ipgapi:avsresponse>PPX</ipgapi:avsresponse>
            <ipgapi:brand>VISA</ipgapi:brand>
            <ipgapi:country>GBR</ipgapi:country>
            <ipgapi:commercialserviceprovider>BOSMS</ipgapi:commercialserviceprovider>
            <ipgapi:orderid>A-966025d3-81a2-453a-820e-bb145e8390d1</ipgapi:orderid>
            <ipgapi:ipgtransactionid>84515799310</ipgapi:ipgtransactionid>
            <ipgapi:paymenttype>CREDITCARD</ipgapi:paymenttype>
            <ipgapi:processorapprovalcode>761862</ipgapi:processorapprovalcode>
            <ipgapi:processorccvresponse>P</ipgapi:processorccvresponse>
            <ipgapi:processorreferencenumber>761862</ipgapi:processorreferencenumber>
            <ipgapi:processorresponsecode>00</ipgapi:processorresponsecode>
            <ipgapi:processorresponsemessage>AUTH CODE:761862</ipgapi:processorresponsemessage>
            <ipgapi:tdate>1521047872</ipgapi:tdate>
            <ipgapi:tdateformatted>2018.03.14 18:17:52 (CET)</ipgapi:tdateformatted>
            <ipgapi:terminalid>21400371</ipgapi:terminalid>
            <ipgapi:transactionresult>APPROVED</ipgapi:transactionresult>
            <ipgapi:transactiontime>1521047872</ipgapi:transactiontime>
         </ipgapi:ipgapiorderresponse>
      </soap-env:body>
   </soap-env:header>
</soap-env:envelope>';

$xml = simplexml_load_string($responseXML);
print_r($xml);
// Returns empty object

我也尝试了以下内容:

$xml = new SimpleXMLElement($responseXML);
print_r($xml);
// This also returns empty object

有人可以帮我弄清楚这有什么问题吗?

1 个答案:

答案 0 :(得分:2)

print_r();与SimpleXMLElement一起使用通常不会提供任何有用的信息。相反,您应该使用输出原始XML的asXML()

$xml = simplexml_load_string($responseXML);
echo $xml->asXML();

哪个给出了

<?xml version="1.0" encoding="UTF-8"?>
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
   <soap-env:header>
      <soap-env:body>
...

您可以使用类似......

之类的内容来访问数据
$xml = simplexml_load_string($responseXML);
$body = $xml->xpath("//soap-env:body");
$bodyData = $body[0]->children("ipgapi", true);
echo $bodyData->ipgapiorderresponse->approvalcode;

所以$bodyData<ipgapi:ipgapiorderresponse ...>元素,因此访问其中的每一部分都是使用最后一行。您可以使用元素名称(减去名称空间前缀ipgapi,因为之前已通过children()调用处理此问题)。该行输出

Y:761862:4515799310:PPXP:0037
相关问题