user2033138 user2033138 - 4 months ago 24
PHP Question

Parse curl_exec() result PHP

I'm working with a Shipping Company web service using curl_exec().
The query result is:

<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="#Oca_Express_Pak">
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="Tarifador" type="xs:int" minOccurs="0" />
<xs:element name="Precio" type="xs:decimal" minOccurs="0" />
<xs:element name="idTiposervicio" type="xs:int" minOccurs="0" />
<xs:element name="Ambito" type="xs:string" minOccurs="0" />
<xs:element name="PlazoEntrega" type="xs:int" minOccurs="0" />
<xs:element name="Adicional" type="xs:decimal" minOccurs="0" />
<xs:element name="Total" type="xs:decimal" minOccurs="0" />
<xs:element name="XML" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<Tarifador>23</Tarifador>
<Precio>383.4100</Precio>
<idTiposervicio>3</idTiposervicio>
<Ambito>Nacional 1</Ambito>
<PlazoEntrega>9</PlazoEntrega>
<Adicional>0.0000</Adicional>
<Total>383.4100</Total>
<XML>&lt;row Tarifador="23" Precio="383.4100"/&gt;</XML>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataSet>


how I can parse the result to get fields "PlazoEntrega" and "Total" ?

Thanks in adavance!

Answer

You could use preg_match().

//$text = your curl result.
    preg_match('/<PlazoEntrega>(.*)<\/PlazoEntrega>/',$text,$matches);
    $plazo = $matches[1];
    preg_match('/<Total>(.*)<\/Total>/',$text,$matches);
    $total = $matches[1];

PHP does have an XML parser:

simplexml_load_file() and simplexml_load_string();
Comments