PHP で有効な xml ファイル (RSS フィード用) を生成する際に問題があります。問題は、空の文字も改行もタブもありませんが、rss リーダー (私の場合は Safari) がエラーをスローすることです。
asdf
私のPHPコードは次のとおりです。
<?php
header("Content-Type: application/rss+xml");
if(!isset($_GET['searchQuery']) || $_GET['searchQuery'] == "")
{
include_once './session_ajax.php';
include("./include.php");
require_once("./classes/myclass/myclass.class.php");
$gmap_ajax = new myclass();
}
$query=sql_query("SELECT item_query, item_date, item_country FROM items WHERE 1 GROUP BY item_query ORDER BY item_id LIMIT 20",1);
$xml = "\n<rss version=\"2.0\">\n";
$xml .= "\t<channel>\n\t\t<title>myClass.com RSS Feed</title>\n\t\t<link>"._siteurl."feed.xml</link>\n\t\t<description>myClass.com is the largest...</description>\n\t\t<language>en-en</language>\n\t\t<pubDate>".date("r")."</pubDate>\n";
while($row=sql_fetch_array($query,1))
{
$date = date("Y-m-d", $row['item_date']);
$item_query2 = $row['item_query'];
$item_query = str_replace(" ", "+", $row['item_query']);
$xml .= "\t\t<item>\n\t\t\t<title>".$item_query2."</title>\n\t\t\t<link>"._siteurl."/".$item_query."/".$row['item_country']."</link>\n\t\t\t<description><![CDATA[Here is some more description]]></description>\n\t\t\t<pubDate>".date("r")."</pubDate>\n\t\t</item>\n";
}
$xml .= "\t</channel>\n</rss>";
echo trim("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
echo $xml;
?>
PHP ファイルでは、<?php タグの前にスペースなどはなく、?> タグの後には何もありません...
Adobe CS5 をエディタとして使用しています。
Safari だけでなく、私が見つけたすべてのオンライン バリデーターでも、次のエラーが発生します。
error on line 1 at column 6: XML declaration allowed only at the start of the document
問題は、ソース コードに空白が表示されることですが、エラーは <?xml タグの "l" にあります。
誰かがこの問題を抱えていて、この問題を解決するための正しい方向へのアイデアやヒントを持っていますか?
編集:
また、Noteped ++とUltraedit32で開いて保存し、サーバーにアップロードしようとしました...同じ問題...