Rss Php And Enclosure Problem

Discussion in 'PHP' started by redbayron2006, Apr 13, 2007.

  1. #1
    Dear friends,
    im trying to create a parser in php to add in my site some infos from an rss feed the below:

    http://www.yikers.com/rss/rss.xml

    i want to input links, titles, descriptions, image. The general form of the feed is like the below:

    <title>Guy Ends Fight By Punching Girl</title>
    <link>http://www.yikers.com/video_guy_ends_fight_by_punching_girl.html</link>
    <guid>http://www.yikers.com/video_guy_ends_fight_by_punching_girl.html</guid>
    <pubDate>Fri, 13 Apr 2007 00:00:00 PDT</pubDate>
    <description>He got praised for that? She should have took a spiked bat to his sac.</description>
    <enclosure url="http://liveu-64.vo.llnwd.net/yikers/flv/thumbs/yikers_guy_ends_fight_by_punching_girl_3.jpg" type="image/jpeg" length="10000"/>
    </item>

    The problem is that the image is in an enlosure weird tag. I dont understand very well the whole rss-parsing thing so ive found the below php code that is able to provide me with all the infos except from the image cause it is in that enclosure tag. I give the code below:

    $MAXLINKCOUNT = 100; // (* 2) if =10, 5 news items will show
    $MAX_DESC_CHARS = 120;

    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    $linkcount = 0;

    function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    $tag = $name;
    } elseif ($name == "ITEM") {
    $insideitem = true;
    }
    }

    function endElement($parser, $name) {

    global $insideitem, $tag, $title, $description, $link, $linkcount, $MAXLINKCOUNT, $MAX_DESC_CHARS;
    if ($name == "ITEM" && $linkcount <= $MAXLINKCOUNT) {
    if (strlen($description) > $MAX_DESC_CHARS) {
    $description = wordwrap($description, $MAX_DESC_CHARS, "-=CUT OFF HERE=-");
    $pos = strpos($description, "-=CUT OFF HERE=-");
    $description = trim(substr($description, 0, $pos)) . "...";
    }
    $description = htmlspecialchars(trim($description));
    $description = str_replace("&amp;quot;", "&quot;", $description);
    printf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),htmlspecialchars(trim($title)));
    printf("<dt>%s</dt><br>",$description);
    $title = "";
    $description = "";
    $link = "";
    $insideitem = false;
    }
    }

    function characterData($parser, $data) {
    global $insideitem, $tag, $title, $description, $link, $linkcount;
    if ($insideitem) {
    switch ($tag) {
    case "TITLE":
    $title .= $data;
    break;
    case "DESCRIPTION":
    $description .= $data;
    break;
    case "LINK":
    $link .= $data;
    $linkcount++;
    break;
    }
    }
    }
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    $fp = fopen("http://www.yikers.com/rss/rss.xml","r")
    or die("Error reading RSS data.");
    while ($data = fread($fp, 2048)) {
    xml_parse($xml_parser, $data, feof($fp));
    }
    fclose($fp);
    xml_parser_free($xml_parser);


    can anyone provide me with the changes that i should do in order to get the image???
    Thanks in advance!
     
    redbayron2006, Apr 13, 2007 IP