0

Im in the process of connecting to a SOAP api, passing some data and then reading the response. Im to the point where I get the SOAP request to send and I get a response but I cannot seem to figure out how to parse the returned XML.

First I pass credentials to receive an auth_ticket:

$rap_soapUrl = "https://technet.rapaport.com/webservices/prices/rapaportprices.asmx?wsdl";
$client = new \SoapClient($rap_soapUrl, array('trace' => 1));
$result = $client->Login($rap_credentials);
$response = $client->__getLastResponse();

This is the point where Im getting stuck. If I echo $response I get

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><AuthenticationTicketHeader xmlns="http://technet.rapaport.com/"><Ticket>TICKETHASH</Ticket></AuthenticationTicketHeader></soap:Header><soap:Body><LoginResponse xmlns="http://technet.rapaport.com/" /></soap:Body></soap:Envelope>

What I need to parse out is TICKETHASH, which is then used in the next request.

I am able to manually parse the string, searching for anything between <Ticket> and </Ticket> but it feels very hacky and not the correct way to handle it.

I tried to use $xml = simplexml_load_string($response); but it returns an empty simplexml object.

Sean S
  • 11
  • 4
  • It's more interesting what `$result` is, because the **SoapClient** already takes care of parsing the XML for you, in SOAP the XML is used for transport. – hakre Mar 26 '15 at 18:24

1 Answers1

-1

simplexml_load_string returns an object of class SimpleXMLElement which you can then use to traverse the XML to parse the tag you want.

This is made slightly harder as <Ticket> lies in the http://technet.rapaport.com/ namespace. However you can use the following:

$xml = simplexml_load_string($str);
$xml->registerXPathNamespace('technet', 'http://technet.rapaport.com/');
$ticket = $xml->xpath('//technet:Ticket');
$ticket_hash = (string)$ticket[0];

print_r($ticket_hash);
thodic
  • 2,229
  • 1
  • 19
  • 35