converting SOAP XML response to a PHP object or array -
i'm using curl send request soap service, send in post body xml containing parameters, in response receive:
web service: http://lcbtestxmlv2.ivector.co.uk/soap/book.asmx?wsdl
<?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:body> <searchresponse xmlns="http://ivectorbookingxml/"> <searchresult> <returnstatus> <success>true</success> <exception /> </returnstatus> <searchurl>http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5&regionid=9</searchurl> <propertyresults> <propertyresult> <totalproperties>215</totalproperties> <propertyid>1795</propertyid> <propertyname>hotel gaddis</propertyname> <rating>3.0</rating> <country>egypte</country> <resort>louxor</resort> <strapline>cet établissement confortable propose un très bon service à un bon rapport qualité-prix. cet hôtel de 6 étages compte 55 chambres et comprend une terrasse, une réception avec coffre-fort et ascenseur,</strapline> <description>cet établissement confortable propose un très bon service à un bon rapport qualité-prix. cet hôtel de 6 étages compte 55 chambres et comprend une terrasse, une réception avec coffre-fort et ascenseur,...</description> <cmsbaseurl>http://lcbtestxml1.ivector.co.uk/content/dataobjects/property/image/</cmsbaseurl> <mainimage>image_1795_v1.jpg</mainimage> <mainimagethumbnail>imagethumb_1795_v1.jpg</mainimagethumbnail> <searchurl>http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5&regionid=9&propertyid=1795</searchurl> <roomtypes> <roomtype> <seq>1</seq> <propertyroomtypeid>690039000</propertyroomtypeid> <mealbasisid>3</mealbasisid> <roomtype>twin/double room</roomtype> <roomview /> <mealbasis>petit déjeuner</mealbasis> <nonrefundablerates>false</nonrefundablerates> <subtotal>150.58</subtotal> <discount>0</discount> <total>150.58</total> <adults>2</adults> <children>1</children> <infants>0</infants> <errata /> </roomtype> <roomtype> <seq>1</seq> <propertyroomtypeid>690039001</propertyroomtypeid> <mealbasisid>7</mealbasisid> <roomtype>twin/double room</roomtype> <roomview /> <mealbasis>demi-pension</mealbasis> <nonrefundablerates>false</nonrefundablerates> <subtotal>291.64</subtotal> <discount>0</discount> <total>291.64</total> <adults>2</adults> <children>1</children> <infants>0</infants> <errata /> </roomtype> <roomtype> <seq>1</seq> <propertyroomtypeid>690039002</propertyroomtypeid> <mealbasisid>5</mealbasisid> <roomtype>double/twin room</roomtype> <roomview /> <mealbasis>pension complète</mealbasis> <nonrefundablerates>false</nonrefundablerates> <subtotal>529.22</subtotal> <discount>0</discount> <total>529.22</total> <adults>2</adults> <children>1</children> <infants>0</infants> <errata /> </roomtype> </roomtypes> </propertyresult> </propertyresults> </searchresult> </searchresponse> </soap:body> </soap:envelope>
i don't have enough experience xml data. spent hours trying convert xml response php object or array, without success.
i need read propertyresults.
php code:
$xml = simplexml_load_string($soap_xml_result); $xml->registerxpathnamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/'); $xml->registerxpathnamespace('xsi', 'http://www.w3.org/2001/xmlschema-instance'); $xml->registerxpathnamespace('xsd', 'http://www.w3.org/2001/xmlschema'); $test = (string) $xml->body->searchresponse->searchresult->searchurl; var_export($test);
the hint of bksi not wrong, technically xml need access namespaced elements properly. works more easy using xpath expression , registering namspace-uri own prefix:
$soap = simplexml_load_string($soapxmlresult); $soap->registerxpathnamespace('ns1', 'http://ivectorbookingxml/'); $test = (string) $soap->xpath('//ns1:searchresponse/ns1:searchresult/ns1:searchurl[1]')[0]; var_dump($test);
output:
string(100) "http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5®ionid=9"
if don't want use xpath, need specify namespace while traverse, children in namespace of element available directly if element not prefixed. root element prefixed first need traverse response:
$soap = simplexml_load_string($soapxmlresult); $response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/') ->body->children() ->searchresponse ;
then can make use of $response
variable know it:
$test = (string) $response->searchresult->searchurl;
because element not prefixed. more complex result returned best because can access response values.
your question similar to:
maybe code/descriptions there helpful, too.
Comments
Post a Comment