Forum Moderators: coopster

Message Too Old, No Replies

reading xml

simply reading xml data in php

         

Seb7

8:30 am on Apr 18, 2011 (gmt 0)

10+ Year Member



ok. I'm using PHP this year, and loving it! However today, I want to grab a value from an RSS XML feed. PHP seems to have an over whelming number of XML functions. An hour later, I'm still dont know were to start! Could someone please convert this example of a made up language in to PHP.. thank you.

$xml = geturl("www.weatherexample.com/london.rss");
$today = $xml.channel.item[0].description.value

Seb7

1:19 pm on Apr 26, 2011 (gmt 0)

10+ Year Member



no one?

eelixduppy

1:55 pm on Apr 26, 2011 (gmt 0)



How about this?


$xml = new SimpleXMLElement(file_get_contents("http://www.weatherexample.com/london.rss"));
$today = $xml->channel->item[0]->description['value'];


It's difficult to know how to construct it as you have not provided any information about the XML structure.

See here for more information: [php.net...]

Seb7

3:32 pm on Apr 26, 2011 (gmt 0)

10+ Year Member



Thank you. not quite perfect, but enough to get me a working solution.
I removed the 'value' bit, then played around with it and changed it to something like this:


$url = "http://www.weatherexample.com/london.rss";
$xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA);
$today = $xml->channel->item[0]->description;

rocknbil

4:06 pm on Apr 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than opening a url, here's a solution using xmlsimple_load_string() and curl. Not sure if it's any better but has error trapping. Will require modification for your usage, "item" is the top tree element.


$xml_err=null;
$url='http://feed.example.com/?key='.DEV_KEY.'&keyword="'.$keyword.'";
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$o=curl_exec($ch);
// Error trapping = best friend
// these are recorded in my function "record_results"
// instead of the error log
libxml_use_internal_errors(true);
$a=simplexml_load_string($o);
//
if (!$a) {
$xml = explode("\n", $o);
$errors = libxml_get_errors();
foreach ($errors as $error) {
$xml_err .= display_xml_error($error, $xml);
}
record_results("XML ERROR: $keyword",$xml_err);
libxml_clear_errors();
}
else {
if ($a->item) { // only if there's "item" in the tree
foreach ($a->item as $v){
$result[]=$v; // array of parsed results
}
}
}


Pretty much from the manual . . . .


function display_xml_error($error, $xml) {
$return = $xml[$error->line - 1] . "\n";
$return .= str_repeat('-', $error->column) . "^\n";
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "Warning $error->code: ";
break;
case LIBXML_ERR_ERROR:
$return .= "Error $error->code: ";
break;
case LIBXML_ERR_FATAL:
$return .= "Fatal Error $error->code: ";
break;
}
$return .= trim($error->message) .
"\n Line: $error->line" .
"\n Column: $error->column";
if ($error->file) {
$return .= "\n File: $error->file";
}
return "$return\n\n--------------------------------------------\n\n";
}