I have a problem generating valid xml file (for rss feed) with PHP. The problem is: there is no empty character nor a linebreak or tab but the rss reader (in my case Safari) throws an error:
asdf
My PHP code is:
<?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 = "
<rss version=\"2.0\">
";
$xml .= "\t<channel>
\t\t<title>myClass.com RSS Feed</title>
\t\t<link>"._siteurl."feed.xml</link>
\t\t<description>myClass.com is the largest...</description>
\t\t<language>en-en</language>
\t\t<pubDate>".date("r")."</pubDate>
";
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>
\t\t\t<title>".$item_query2."</title>
\t\t\t<link>"._siteurl."/".$item_query."/".$row['item_country']."</link>
\t\t\t<description><![CDATA[Here is some more description]]></description>
\t\t\t<pubDate>".date("r")."</pubDate>
\t\t</item>
";
}
$xml .= "\t</channel>
</rss>";
echo trim("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
echo $xml;
?>
In the PHP file there is no space or anything else before the <?php tag and nothing after the ?> tag...
I'm using Adobe CS5 as editor.
In Safari as well as in every online validator I found I get this error:
error on line 1 at column 6: XML declaration allowed only at the start of the document
The problem is, I can see the empty space in the source code but the error is located at the "l" of the <?xml tag.
Does anybody have had this problem and has an idea or just a hint to the right direction to solve this problem?
Edit:
I've also tried to open and save it with Noteped++ and Ultraedit32 and then upload it to the server... same problem...
Check your echo trim, do you see the php tags, the first is inside the quotes and the closing one is outside the quotes, there's your problem.
Check your output (in a text/hex editor) for whitespace.
If you cannot see anything, your problem is likely that you are generating a BOM. Use a tool like File BOM Detector (Windows) to scan your files (remember includes and their includes).
Thank you all for your answeres! Specially thank you, Deebster.
The solvation was so simple:
include("./include.php");
In this file I found the empty space!
I'm so sorry that I wasted your time but if somebody else covers this problem:
Also if you're absolutly sure, control every included file twice...!
Thnx. agian and best regards to all!
Ingmar
After checking all my files manually, I couldn't get rid of the error. I'm using Dreamweaver as my text editor. I used the "Apply Source Formatting" function and it removed white spaces for me. That got rid of the error