Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cURL, extract an XML response

Tags:

php

curl

I am invoking PHP cURL method on a server and the response is XML type. cURL is saving the output (after removing the tags) in a scalar type variable. Is there a way to store it in an object/hash/array so that it's easy to parse?

like image 600
Rakesh Avatar asked Feb 18 '09 16:02

Rakesh


People also ask

How to GET XML response from Curl PHP?

Just add header('Content-type: application/xml'); before your echo of the XML response and you will see an XML page.

How do I get curl response in XML?

To send XML to the server using Curl, you need to pass the XML data to Curl with the -d command line option and specify the data type in the body of the POST message using the -H "Content-Type: application/xml" command-line option.

How to send XML request using Curl in PHP?

To post XML using Curl, you need to pass XML data to Curl with the -d command line parameter and specify the data type in the body of the POST request message using the -H Content-Type: application/xml command line parameter.


2 Answers

<?php function download_page($path){     $ch = curl_init();     curl_setopt($ch, CURLOPT_URL,$path);     curl_setopt($ch, CURLOPT_FAILONERROR,1);     curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);     curl_setopt($ch, CURLOPT_TIMEOUT, 15);     $retValue = curl_exec($ch);               curl_close($ch);     return $retValue; }  $sXML = download_page('http://alanstorm.com/atom'); $oXML = new SimpleXMLElement($sXML);  foreach($oXML->entry as $oEntry){     echo $oEntry->title . "\n"; } 
like image 197
Alan Storm Avatar answered Sep 18 '22 12:09

Alan Storm


Just add header('Content-type: application/xml'); before your echo of the XML response and you will see an XML page.

like image 25
arkleyjoe Avatar answered Sep 18 '22 12:09

arkleyjoe