PHP - 无法读取xml子节点值

时间:2016-06-27 13:17:47

标签: php xml curl soap wsdl

我用curl调用这个soap API并且无法正确读取响应值所以请你指导我做错了什么。

这是我的代码:

$wsdl = "http://xxxx/xxxx/xxxx";
$body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:par="http://xxxx.com">
   <soapenv:Header/>
   <soapenv:Body>
      <par:cancelReservationReq>
         <par:garageID>47</par:garageID>
         <par:orderNumber>orderNumber168</par:orderNumber>
         <!--Optional:-->
         <par:barCode>barCode168</par:barCode>
         <!--Optional:-->
         <par:orderTime>2015-11-19T00:30:57.905</par:orderTime>
      </par:cancelReservationReq>
   </soapenv:Body>
</soapenv:Envelope>';

// initializing cURL with the IPG API URL:
$ch = curl_init($wsdl);

// setting the request type to POST:
curl_setopt($ch, CURLOPT_POST, 1);

// setting the content type:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));

// setting the authorization method to BASIC:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

// filling the request body with your SOAP message:
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

// telling cURL to verify the server certificate:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

// telling cURL to return the HTTP response body as operation result
// value when calling curl_exec:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// calling cURL and saving the SOAP response message in a variable which
// contains a string like "<SOAP-ENV:Envelope ...>...</SOAP-ENV:Envelope>":

$result = curl_exec($ch);

// loads the XML
$xml = simplexml_load_string($result);

$soapenv = $xml->children("http://schemas.xmlsoap.org/soap/envelope/");
$tns = $soapenv->Body->children("http://xxxx.com");

echo "success ".$success = $tns->cancelReservationResp->success;
echo "message ".$message = $tns->cancelReservationResp->message;
echo "garageID ".$garageID = $tns->cancelReservationResp->reservations->garageID;

API响应数据:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body xmlns:s="http://www.w3.org/2003/05/soap-envelope">
      <tns:cancelReservationResp xmlns:tns="http://xxxx.com">
         <tns:success>true</tns:success>
         <tns:message/>
         <tns:reservations>
            <tns:garageID>47</tns:garageID>
            <tns:barCode>barCode168</tns:barCode>
            <tns:licensePlate>licensePlate168</tns:licensePlate>
            <tns:orderNumber/>
            <tns:used>false</tns:used>
            <tns:cancelled>true</tns:cancelled>
            <tns:startTime>2015-11-20 22:53:45.547</tns:startTime>
            <tns:endTime>2015-11-21 22:53:45.547</tns:endTime>
            <tns:cancelledTime>2015-11-19 00:51:58.717</tns:cancelledTime>
            <tns:checkInTime/>
            <tns:checkOutTime/>
            <tns:grossPrice>152.6000</tns:grossPrice>
            <tns:commFee>162.6000</tns:commFee>
            <tns:customerName>customerName168</tns:customerName>
            <tns:customerEmail>customerEmail168</tns:customerEmail>
            <tns:customerPhone>customerPhone168</tns:customerPhone>
            <tns:vehicleMake>vehicleMake168</tns:vehicleMake>
            <tns:vehicleModel>vehicleModel68</tns:vehicleModel>
            <tns:vehicleColor>vehicleColor168</tns:vehicleColor>
            <tns:reentryAllowed>true</tns:reentryAllowed>
         </tns:reservations>
      </tns:cancelReservationResp>
   </soapenv:Body>
</soapenv:Envelope>

我从<tns:success>true</tns:success>变量获取此$success值,但未从$garageID变量中获取任何内容。

知道为什么$garageID变空了?

感谢。

0 个答案:

没有答案