tags:

views:

4521

answers:

2

hello, 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 a object/hash/array. So that its easy to parse.\

Thanks

+1  A: 

no, CURL does not have anything with parsing XML, it does not know anything about the content returned. it serves as a proxy to get content. it's up to you what to do with it.

use JSON if possible (and json_decode) - it's easier to work with, if not possible, use any XML library for parsin such as DOMXML: http://php.net/domxml

dusoft
I agree to that . But why are tags not shown. I do an echo . Is it because of browser?
Rakesh
yes, browser treats XML as (X)HTML, so if > or < is encountered, it is treated as normal tag...
dusoft
If you echo htmlentities(...) you'll see the full XML
Greg
or just view source to see XML...
dusoft
yep. That works. Thanks all
Rakesh
+6  A: 
<?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";
}
Alan Storm