0

I am trying call SOAP API request using CURL and here is my code:

$wsdl = "http://xxxxx//xxxxx//xxxxx";
$body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:par="http:///xxxxx.com">
   <soapenv:Header/>
   <soapenv:Body>
      <par:createReservationReq>
         <par:garageID>47</par:garageID>
         <par:orderNumber>orderNumber'.$id.'</par:orderNumber>
         <!--1 or more repetitions:-->
         <par:vehicles>
            <par:barCode>barCode'.$id.'</par:barCode>
            <!--Optional:-->
            <par:licensePlate>licensePlate'.$id.'</par:licensePlate>
            <par:startTime>2015-11-20T22:53:45.547</par:startTime>
            <par:endTime>2015-11-21T22:53:45.547</par:endTime>
            <!--Optional:-->
            <par:vehicleMake>vehicleMake'.$id.'</par:vehicleMake>
            <!--Optional:-->
            <par:vehicleModel>vehicleMode'.$id.'</par:vehicleModel>
            <!--Optional:-->
            <par:vehicleColor>vehicleColor'.$id.'</par:vehicleColor>
         </par:vehicles>
         <!--Optional:-->
         <par:grossPrice>152.6</par:grossPrice>
         <!--Optional:-->
         <par:commFee>162.6</par:commFee>
         <!--Optional:-->
         <par:customerName>customerName'.$id.'</par:customerName>
         <!--Optional:-->
         <par:customerEmail>customerEmail'.$id.'</par:customerEmail>
         <!--Optional:-->
         <par:customerPhone>customerPhone'.$id.'</par:customerPhone>
         <!--Optional:-->
         <par:reentryAllowed>true</par:reentryAllowed>
      </par:createReservationReq>
   </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);

Now I don't understand how to read value from xml response. I am adding my response data here so you can get proper idea:

<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:createReservationResp xmlns:tns="http://xxxxx.com">
         <tns:success>true</tns:success>
         <tns:message>Test Garage order orderNumber168. created 2015-11-19 00:30:57.905</tns:message>
         <tns:orderTime>2015-11-19 00:30:57.905</tns:orderTime>
         <tns:reservations>
            <tns:garageID>47</tns:garageID>
            <tns:barCode>barCode168</tns:barCode>
            <tns:licensePlate>licensePlate168</tns:licensePlate>
           <tns:orderNumber>orderNumber168</tns:orderNumber>
            <tns:used>false</tns:used>
            <tns:cancelled>false</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/>
            <tns:checkInTime/>
            <tns:checkOutTime/>
            <tns:grossPrice>152.6</tns:grossPrice>
            <tns:commFee>162.6</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:createReservationResp>
   </soapenv:Body>
</soapenv:Envelope>

I want to read this value from response:

<tns:success>true</tns:success>
<tns:message>Test Garage order orderNumber168. created 2015-11-19 00:30:57.905</tns:message>
<tns:orderTime>2015-11-19 00:30:57.905</tns:orderTime>

<tns:garageID>47</tns:garageID>
<tns:barCode>barCode168</tns:barCode>

Any Idea?

Thanks.

Mr.Happy
  • 2,639
  • 9
  • 40
  • 73

1 Answers1

1

In general you should use SoapClient when dealing with SOAP instead of curl. It provides much cleaner interface. In your case one needs to work with SimpleXML functions, a good tutorial is on the SitePoint.

$soapenv = $xml->children("http://schemas.xmlsoap.org/soap/envelope/");
$tns = $soapenv->Body->children("http://xxxxx.com");
echo "success: " . $tns->createReservationResp->success . "\n";
echo "message: " . $tns->createReservationResp->message . "\n";
echo "orderTime: " . $tns->createReservationResp->orderTime . "\n";
echo "garageID: " . $tns->createReservationResp->reservations->garageID . "\n";
echo "barCode: " . $tns->createReservationResp->reservations->barCode . "\n";
Ruslan Bes
  • 2,715
  • 2
  • 25
  • 32
  • Can you please help on this topic: `http://stackoverflow.com/questions/38055023/php-not-able-to-read-xml-sub-node-value` if possible. – Mr.Happy Jun 27 '16 at 13:20