简体   繁体   中英

Rss Feed, generating the image

I am trying to generate an RSS feed on my site using the code below. The rss is appearing but I am having two issues:

  1. When the feed shows on my page the images do not show up, instead you see the img link appear directly on the page like this... <a href="http://movies.nytimes.com/2011/11/18/movies/garbo-the-spy-about-juan-pujol-garcia-review.html?partner=rssnyt&emc=rss"><img src="http://graphics8.nytimes.com/images/2011/11/18/movies/18RDP_GARBO/18RDP_GARBO-thumbStandard.jpg" border="0" height="75" width="75" hspace="4" align="left"></a>

  2. How do I limit the amount of articles that appear on my site?

Here is the link to the RSS: Spy RSS FEED Here is the code I am using:

    <?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$locations = array('http://topics.nytimes.com/topics/reference/timestopics/subjects/e/espionage/index.html?rss=1');
srand((float) microtime() * 10000000); // seed the random gen 
$random_key = array_rand($locations);
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;
 if ($name == "ITEM") {
  printf("<dt><b><a href='%s' target=new>%s</a></b></dt>",
  trim($link),htmlspecialchars(trim($title)));
  printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description)));
  $title = "";
  $description = "";
  $link = "";
  $insideitem = false;
 }
}
function characterData($parser, $data) {
 global $insideitem, $tag, $title, $description, $link;
 if ($insideitem) {
 switch ($tag) {
  case "TITLE":
  $title .= $data;
  break;
  case "DESCRIPTION":
  $description .= $data;
  break;
  case "LINK":
  $link .= $data;
  break;
 }
 }
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($locations[$random_key], 'r')
 or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
 xml_parse($xml_parser, $data, feof($fp))
  or die(sprintf("XML error: %s at line %d",
   xml_error_string(xml_get_error_code($xml_parser)),    
   xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>

In endElement() , when outputting the feed content, it calls printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description)));

If you remove the htmlspecialchars function, then it should display images and other html properly instead of converting < to &lt; etc.

Given that code, there is no built in way to limit the number of feeds. Nytimes may have an option you can pass as part of the query string that restricts the number of results, but I am not sure about that.

A quick fix would be to add a global variable called $numShown or something like that, and at the beginning of endElement() , you can increment it, and the check to see if it is above some value and if so just return prior to all the printf calls to output the feed item.

<?php

function endElement($parser, $name) {
    global $insideitem, $tag, $title, $description, $link, $numShown;

    if ($name == "ITEM") {
        $numShown++;

        if ($numShown >= 5) {
            return ;
        }

        printf ( "<dt><b><a href='%s' target=new>%s</a></b></dt>", trim ( $link ), htmlspecialchars ( trim ( $title ) ) );
        printf ( "<dt>%s</dt><br><br>", trim ( $description ) );
        $title = "";
        $description = "";
        $link = "";
        $insideitem = false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM