Jump to content

Getting a URL from XML for img src


jesse_james

Recommended Posts

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();
?>" />

 

But of course that didn't work. How can I get data from an XML file placed nicely into an img src=""?  Is there any way to bypass XSLT and bring data straight from an XML sheet into a PHP file?

 

Link to comment
https://forums.phpfreaks.com/topic/163073-getting-a-url-from-xml-for-img-src/
Share on other sites

First of all, I'm a bit disappointed that no one in this forum contributed to the topic over the last few days.  But it does seem to be a busy forum.

 

Thankfully, someone over on the Digital Point PHP forum submitted a working solution, the code for which I have pasted below.

 

<?
$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!";
}
?>

 

Just in case someone stumbled upon this post later on and needed the answer (as I often do with forums), I thought I would leave it for them.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.