I'm familiar with putting PHP into an img src="" to grab an image URL from mySQL, but I need to do something a bit different. As you can see on the Yahoo weather RSS page for Chad ( feed://weather.yahooapis.com/forecastrss?p=CDXX0003&u=f ), they have a picture icon for the current weather. I'm using this RSS feed to set up my own weather page on Chad, and I want to use their icon. I tried using XSL transfer in a img src like this: <img src="<?php $mm_xsl = new MM_XSLTransform(); $mm_xsl->setXML("http://weather.yahooapis.com/forecastrss?p=CDXX0003&u=f"); $mm_xsl->setXSL("x/xsl/pic.xsl"); echo $mm_xsl->Transform(); ?>" /> Code (markup): But of course that didn't work. How can I get data from an XML file placed nicely into an img src=""?
I'm afraid that won't work, but tell me if I'm wrong. Since I wasn't familiar with xml_parser when I read your reply, I went straight to w3schools.com to read up. Their XML Parser guide says that "For security reasons, modern browsers do not allow access across domains. This means, that both the web page and the XML file it tries to load, must be located on the same server." And since the xml file is hosted by Yahoo, I couldn't use it with the XML parser function. This is really aggravating. I can easily get the URL to display in a XSL file, and I can have it show up as text on a .php page, but there's no way to put it in a img src""??????
Try the following code which works fine for me. You have the option of echoing the image or just the image URL. As two arrays are created by preg_match_all() one of which contains the image with <img src=" "/> while the other just contains the URL. You can also use preg_match_all() to access the other weather info if necessary. <? $feed = file_get_contents('http://weather.yahooapis.com/forecastrss?p=CDXX0003&u=f'); preg_match_all('/<img src="(.*)"\/>/', $feed, $image); if ($image !== false) { //Echo image echo $image[0][0]; echo "<p>"; //Echo image URL echo $image[1][0]; } else { echo "Image Not Available!"; } ?> Code (markup):
Excellent! The code works flawlessly! A big thanks to user myhart and his perfect contribution. I also posted this at phpfreaks.com and got no help. Digital Point all the way!
This would have worked had there been a picture in the xml data <?php $xml = simplexml_load_file('http://weather.yahooapis.com/forecastrss?p=CDXX0003$') var_dump($xml); echo '<img src="'.$xml->item->image->url.'">'; ?> PHP: