0

我正在使用以下 php 代码创建 XML 文件:

    echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>mytitle</title>
<description>my description</description>
<link>www.mysite.com</link>';

$sql = "SELECT title, description, url, picture, formatted_date 
FROM somewhere" ;

$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

if(mysql_num_rows($result)>0)
{
   while($result_array = mysql_fetch_assoc($result))
   {
    //timestamp date to xml format
     $pubdate = date('D, d M Y H:i:s O', strtotime($result_array[formatted_date]));

         echo '  
       <item>  
          <title>'.$result_array[title].'</title>
          <description>'.$result_array[description].'</description> 
          <link>'.$result_array[indit_url].'</link>  
          <pubDate>'.$pubdate.' GMT</pubDate> 

          <enclosure url="'.$result_array[picture].'" length="1121990" type="image"/>
            <image>
              <url>'.$result_array[picture].'</url> 
               <title>image title</title>   
               <link>'.$result_array[picture].'</link>         
                 <width>111</width>                             
               <height>33</height>                             
               <description>An amazing picture</description>   
   </image>      </item>'; 

使用http://validator.w3.org/正确验证了创建的文件,但是当我尝试使用一些 xml 解析器时,例如:http://simplepie.org/,perser 无法捕获图像。我是否使用正确的标签将图像插入 xml 文件?是否有任何其他标签或方法来插入图像?

4

2 回答 2

0

请尝试转义图像标签中的特殊字符。

使用htmlspecialchars$result_array[picture]

另外,您能否在此处显示您的 rss 提要示例,以便答案更准确。

于 2013-03-27T17:53:49.640 回答
0

我建议将数据包围在 XML 转义结构中 - 例如:

<image>
    <url><![CDATA[ imagedatagoeshere ]]></content></url>
</image>

但是,我不熟悉 simplepie,所以我不知道它是否正确理解 CDATA。

于 2013-03-27T18:00:28.903 回答