2015-03-09 3 views
0

Я пытаюсь преобразовать ответ XML через CURL в массив в PHP. Я пробовал с приведенным ниже кодом, но не получал ожидаемый массив результатов.Преобразование XML-ответа CURL в массив в PHP

С помощью simplexml_load_string:

$xml = simplexml_load_string($response); 
$json = json_encode($xml); 
$arr = json_decode($json,true); 
print_r($arr);//giving array() empty 

С помощью SimpleXMLElement:

$xml = new SimpleXMLElement($response); 
print_r($xml); //giving SimpleXMLElement Object () empty array 

Мой Curl ответ:

<?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" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"> 
    <soap:Header> 
    <OGHeader transactionID="00239870" timeStamp="2009-02-23T01:55:01.4625+05:30" primaryLangID="E" xmlns="http://webservices.micros.com/og/4.3/Core/"> 
     <Origin entityID="WEST" systemType="ORS" /> 
     <Destination entityID="OWS" systemType="WEB" /> 
    </OGHeader> 
    <wsa:Action>http://webservices</wsa:Action> 
    <wsa:MessageID>urn:uuid:a9a70c23-3d94-4640-9aac-8ac63694733a</wsa:MessageID> 
    <wsa:RelatesTo>urn:uuid:eb565d90-b682-45e9-b18d-c03fa7323019</wsa:RelatesTo> 
    <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To> 
    </soap:Header> 
    <soap:Body> 
    <CreateBookingResponse xmlns:r="http://webservices" xmlns:hc="http://webservices/" xmlns:c="http://webservices" xmlns="http://webservices"> 
     <Result resultStatusFlag="FAIL"> 
     <c:Text> 
      <c:TextElement></c:TextElement> 
     </c:Text> 
     <c:OperaErrorCode>PRIOR_STAY</c:OperaErrorCode> 
     </Result> 
    </CreateBookingResponse> 
    </soap:Body> 
</soap:Envelope> 

ответ

0

Попробуйте вместо этого:

<?php 
$xml = simplexml_load_string($response); 
var_dump($xml->asXML()); 

Ваш xml есть, как вы можете видеть.

SimpleXml реализует ArrayIterator, поэтому вы можете перемещаться с помощью foreach или использовать любой из методов SimpleXmlElement перемещаться по нему (например, children() или xpath()).

+0

это печать: '

string'<?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" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"><soap...entityID="OWS" systemType="W'...(length=1322) 
' –