开发者

How to display image in RSS?

I am trying to write a very simple RSS channel which would display the current day's comic strip from sinfest.net, but I cannot force it to display anything but the linked title. Link to one of the code versions:

<?php
$page = file_get_contents('http://www.sinfest.net/index.php');
$title = '';
$description = '';
$link = '';
$date = date("Y-m-d");
if (preg_match('~<img src="(http://sinfest\\.net/comikaze/comics/.*\\.gif)" alt="(.*)" border="0" />~isU', $page, $match)) {
        $title = $match[2];
        $description = "<img src='{$match[1]}'/>";
}
if (preg_match('~<a href="http://sinfest\\.net/archive_page\\.php\\?comicID=([0-9]*)"><img src="images/prev_a.gif"~isU', $page, $match)) {
        $link = 'http://sinfest.net/archive_page.php?comicID=' . ($match[1]+1);
}
$ok = $title && $description && $link;
$image = "http://www.sinfest.net/comikaze/comics/" . $date . ".gif";
echo '<?xml version="1.0" encoding="ISO-8859-1" ?>';
echo '<rss version="2.0">
        <channel>
                <title>Latest Sinfest</title>
                <link>http://www.sinfest.net/</link>
                <description>Latest Sinfest</description>
                <image>
                      <url>' . $image . '</url>
                      <title>' . htmlspecialchars($title) . '</title>
                      <link>' . htmlspecialchars($link) . '</link>
                </image>';
if ($ok):
echo '               <item>
                        <title>' . htmlspecialchars($title) . '</title>
                        <link>' . htmlspecialchars($link) . '</link>
                        <description><img src="' . $image . '" /></description>
                        <enclosure url="' . $image . '" type="image/jpeg" />
                </item>';
elseif (!isset($_GET['noerror'])): 
echo '                <item>
                        <title>Error parsing news.' . date('Y-m-d H:i:s') . '</title>
                        <link>about:blank</link>
                        <description>Error parsing news.</description>
                </item>';
endif;
echo '        </channel>
</rss>';
?>

The RSS-only code (I didn't remove the PHP variables):

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
    <channel>
        <title>Latest Sinfest</title>
        <link>http://www.sinfest.net/</link>
        <description>Latest Sinfest</description>
        <image>
            <url>' . $image . '</url>
            <title>' . htmlspecialchars($title) . '</title>
            <link>' . htmlspecialchars($link) . '</link>
        </image>
        <item>
            <title>' . h开发者_开发问答tmlspecialchars($title) . '</title>
            <link>' . htmlspecialchars($link) . '</link>
            <description><img src="' . $image . '" /></description>
            <enclosure url="' . $image . '" type="image/jpeg" />
        </item>
    </channel>
</rss>

Any idea what am I doing wrong and some piece of advice maybe? Thank is advance.


Your best bet is to CDATA that image section (alternatively you can "htmlentities()" it, but this is better!)

<description><![CDATA[<img src="' . $image . '" />]]></description>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜