我有以下 SOAP 响应。我尝试使用以下代码来获取以下标签的值:
<ns2:paymenttransactionid>TX00TTP1HLS51QSR</ns2:paymenttransactionid>
如何提取标签之间的值?
<body cz-shortcut-listen="true" data-new-gr-c-s-check-loaded="14.1153.0" data-gr-ext-installed="">
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:header>
<soap-env:body>
<ns2:paymentserviceresponse xmlns:ns2="http://www.allianz-assistance.com/emagin/services/PaymentService/1.0" xmlns:ns3="http://www.allianz-assistance.com/emagin/services/PaymentService/common/1.0" version="1.0">
<ns2:globalrequestid>paymentOrder</ns2:globalrequestid>
<ns2:date>2024-02-05T08:09:45.968Z</ns2:date>
<ns2:userid>AWC_TT</ns2:userid>
<ns2:accountid>ACCOUNT_AWC_TT_MOTO</ns2:accountid>
<ns2:paymentoperation>Authorize</ns2:paymentoperation>
<ns2:paymentmethod>PaymentCard</ns2:paymentmethod>
<ns2:customerinformation>
<ns2:partnercode>ALS</ns2:partnercode>
<ns2:bucode>CH</ns2:bucode>
</ns2:customerinformation>
<ns2:paymentstatus>
<ns2:paymenttransactionid>TX00TTP1HLS51QSR</ns2:paymenttransactionid>
<ns2:orderstatus>Success</ns2:orderstatus>
<ns2:orderid>RD00TTO1HLS51QEP</ns2:orderid>
<ns2:orderreason>ORDER_APPROVED</ns2:orderreason>
<ns2:paymenttransactionstatus>AUTHORIZED</ns2:paymenttransactionstatus>
<ns2:providerpaymentstatus code="WorldPayDXML">
<ns2:provideraccountid>AWCTALENTTRUSTMOTO</ns2:provideraccountid>
<ns2:providertransactionid>TX00TTP1HLS51QSR</ns2:providertransactionid>
<ns2:providerorderid>PR00TTP1HLS51QSR</ns2:providerorderid>
<ns2:providerreason> (CVC status: )</ns2:providerreason>
</ns2:providerpaymentstatus>
</ns2:paymentstatus>
</ns2:paymentserviceresponse>
</soap-env:body>
</soap-env:header>
</soap-env:envelope>
</body>
echo $response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('ns2', 'http://www.allianz assistance.com/emagin/services/PaymentService/1.0');
foreach ($xml->xpath('//ns2:paymenttransactionid') as $item)
{
echo '<pre>' . $item . '</pre>';
}
页面上没有显示值TX00TTP1HLS51QSR,也没有报错?
^_^ 你刚刚忘记了 -
$dummyRes='
<body cz-shortcut-listen="true" data-new-gr-c-s-check-loaded="14.1153.0" data-gr-ext-installed="">
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:header>
<soap-env:body>
<ns2:paymentserviceresponse xmlns:ns2="http://www.allianz-assistance.com/emagin/services/PaymentService/1.0" xmlns:ns3="http://www.allianz-assistance.com/emagin/services/PaymentService/common/1.0" version="1.0">
<ns2:globalrequestid>paymentOrder</ns2:globalrequestid>
<ns2:date>2024-02-05T08:09:45.968Z</ns2:date>
<ns2:userid>AWC_TT</ns2:userid>
<ns2:accountid>ACCOUNT_AWC_TT_MOTO</ns2:accountid>
<ns2:paymentoperation>Authorize</ns2:paymentoperation>
<ns2:paymentmethod>PaymentCard</ns2:paymentmethod>
<ns2:customerinformation>
<ns2:partnercode>ALS</ns2:partnercode>
<ns2:bucode>CH</ns2:bucode>
</ns2:customerinformation>
<ns2:paymentstatus>
<ns2:paymenttransactionid>TX00TTP1HLS51QSR</ns2:paymenttransactionid>
<ns2:orderstatus>Success</ns2:orderstatus>
<ns2:orderid>RD00TTO1HLS51QEP</ns2:orderid>
<ns2:orderreason>ORDER_APPROVED</ns2:orderreason>
<ns2:paymenttransactionstatus>AUTHORIZED</ns2:paymenttransactionstatus>
<ns2:providerpaymentstatus code="WorldPayDXML">
<ns2:provideraccountid>AWCTALENTTRUSTMOTO</ns2:provideraccountid>
<ns2:providertransactionid>TX00TTP1HLS51QSR</ns2:providertransactionid>
<ns2:providerorderid>PR00TTP1HLS51QSR</ns2:providerorderid>
<ns2:providerreason> (CVC status: )</ns2:providerreason>
</ns2:providerpaymentstatus>
</ns2:paymentstatus>
</ns2:paymentserviceresponse>
</soap-env:body>
</soap-env:header>
</soap-env:envelope>
</body>
';
$response = $dummyRes;
$xml = simplexml_load_string($response);
//replace this:
//$xml->registerXPathNamespace('ns2', 'http://www.allianz assistance.com/emagin/services/PaymentService/1.0');
//with this:
$xml->registerXPathNamespace('ns2', 'http://www.allianz-assistance.com/emagin/services/PaymentService/1.0');
//^_^ have a nice day
foreach ($xml->xpath('//ns2:paymenttransactionid') as $item)
{
echo '<pre>' . $item . '</pre>';
}