Getting a url from XML for img src

Discussion in 'PHP' started by mcarter, Jun 20, 2009.

  1. #1
    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=""?
     
    mcarter, Jun 20, 2009 IP
  2. HorseGalleria

    HorseGalleria Peon

    Messages:
    91
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could use xml_parser to pull out the description element. From that, pull out the img tag.
     
    HorseGalleria, Jun 21, 2009 IP
  3. mcarter

    mcarter Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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""??????
     
    mcarter, Jun 21, 2009 IP
  4. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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):
     
    myhart, Jun 22, 2009 IP
  5. mcarter

    mcarter Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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!
     
    mcarter, Jun 23, 2009 IP
  6. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #6
    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:
     
    dweebsonduty, Jun 27, 2009 IP