Question

I have a problem with querying a web service (Yahoo weather). Thanks to this cool forum, I found the following hint. Nevertheless, I am not able to retrieve my value.

I am extracting the CDATA part by using

$conditionIcon = $weatherXmlObject->xpath("//item/description");
$dom = new DOMDocument();
$dom->loadHTML($conditionIcon); // or you can use loadXML
$xml = simplexml_import_dom($dom);
$imgSrc = (string)$xml->body->img['src'];
echo $imgSrc;

$imgSrc is always empty.

Description looks like this

<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 50 F<BR />
<BR /><b>Forecast:</b><BR />
Fri - Partly Cloudy. High: 62 Low: 49<br />
Sat - Partly Cloudy. High: 65 Low: 49<br />
<br />
<a    href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
Was it helpful?

Solution 2

OK, I solved the issue but using a regex (thanks to this page where the issue was discussed, too):

// retrieve link to condition image - in description element
$xml = simplexml_load_string($weather_feed, 'SimpleXMLElement', LIBXML_NOCDATA); 
$description = $xml->channel->item->description;
//preg match regular expression to extract weather icon
$imgpattern = '/src="(.*?)"/i';
preg_match($imgpattern, $description, $matches);
$aExport['img_url'] = $matches[1];

Cheers

OTHER TIPS

The problem is that the parser ignores all data within a CDATA block. You must load the body of the description in another DOMDocument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top