Page 1 of 1

PHP produced XML giving extra lines at start of doc

Posted: Fri Oct 20, 2006 10:56 am
by intergroove
I'm still struggling with this problem.

My code seems to work on a Windows 2003 server but on Linux it is producing an extra blank line or two before the xml declaration.

Here is my post.php code:

Code: Select all

<?
	$method=$_GET['method']; $id=$_GET['id']; $title=$_GET['title']; $description=$_GET['description'];
	require_once("../classes/Post.class.php");
	$post = new Post();
	$post->$method($id, $title, $description);
?>

This calls my Post class get() function to produce the XML.
Here is that code:

Code: Select all

function get()
	{
		$this->dbConnect();
		$query = "SELECT * FROM $this->table ORDER BY id desc";
		$result = mysql_db_query (DB_NAME, $query, LINK);

		$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
		$xml .= "<posts>\n";
		while($row = mysql_fetch_array($result))
		{
			$xml .= "<post>\n";
			$xml .= "<id>" . $row['id'] . "</id>\n";
			$xml .= "<date>" . $row['date'] . "</date>\n";
			$xml .= "<title><![CDATA[" . $row['title'] . "]]></title>\n";
			$xml .= "<description><![CDATA[" . $row['description'] . "]]></description>\n";
			$xml .= "</post>\n";
		}
		$xml .= "</posts>";
		mysql_close();
		
		header("Content-Type: application/xml; charset=UTF-16");
		echo $xml;
	}
As you can see there are no spaces or returns before the <?xml line and none in the post.php file, but I am getting a 'XML Declaration not at start of external entity' error.

When I copy & paste the output XML I get the following:

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1" ?>
<posts>
<post>
<id>11</id>
<date>2006-10-20 03:02:01</date>
<title><![CDATA[Testing #2]]></title>
<description><![CDATA[,mh,mwen.fdwe fe wef ewf ewf wef we]]></description>
</post>
<post>
<id>1</id>
<date>2006-10-19 01:02:03</date>

<title><![CDATA[Testing #1]]></title>
<description><![CDATA[mn kjdbsvsvd vsdvkjdsv dsvovj dvkjodsvkm sdkvoubsdv  sv]]></description>
</post>
</posts>
Here you can see there is one line extra at the top of the XML.

I can't reconcile why the very same code would work on one server and not on the next. This has had me chasing my tail for days now. It's only a simple piece of code, but I need to prove the method works before I launch into bulding my project.

Posted: Fri Oct 20, 2006 10:58 am
by RobertGonzalez
Since the XML output is being echo'd, have you made sure there are no empty lines at the beginning of the PHP script?

Posted: Fri Oct 20, 2006 11:00 am
by intergroove
I just noticed - the CHARSET used is UTF-8 - I was experimenting with a different CHARSET (16) to see the result - so ignore that in the PHP above

Posted: Fri Oct 20, 2006 11:06 am
by intergroove
Everah wrote:Since the XML output is being echo'd, have you made sure there are no empty lines at the beginning of the PHP script?
Yes. I have been around the scripts with the delete button. An errant space or CR is the obvious primary culprit - but days of pouring over the script says no - that is not the cause.

Posted: Fri Oct 20, 2006 11:08 am
by RobertGonzalez
Is there any chance that something else somewhere in the script could be throwing an empty line out? Maybe like a debugger or something? It is possible this is an issue with the Windows server, but I would tend to look toward the complete code output before looking at the server.

EDIT | Sorry, my post came in after your post above.

Posted: Fri Oct 20, 2006 11:14 am
by intergroove
As I said, I've been through the code with a fine toothcomb lookign for anything that could throw an extra line out - the fact that the code works on a Windows Server and not on a Linux one has got me perplexed.

Here is the post.php link using the get() method to draw the info.
http://www.kickinmusic.com/sns/ajax/ser ... method=get

Thanks.

Posted: Fri Oct 20, 2006 11:15 am
by RobertGonzalez
Dude, now it looks like two extra lines. Can you post the entire code for the page that produces that?

Posted: Fri Oct 20, 2006 11:18 am
by intergroove
Here is the Post Class that produces the XML

Code: Select all

<?php

class Post
{
	var $table;
	
	function Post()
	{
		require_once('../dbconn.php');
		$this->table = "testajax";
	}
	
	function dbConnect()
	{
		DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
	}
	
	function get()
	{
		$this->dbConnect();
		$query = "SELECT * FROM $this->table ORDER BY id desc";
		$result = mysql_db_query (DB_NAME, $query, LINK);

		$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
		$xml .= "<posts>\n";
		while($row = mysql_fetch_array($result))
		{
			$xml .= "<post>\n";
			$xml .= "<id>" . $row['id'] . "</id>\n";
			$xml .= "<date>" . $row['date'] . "</date>\n";
			$xml .= "<title><![CDATA[" . $row['title'] . "]]></title>\n";
			$xml .= "<description><![CDATA[" . $row['description'] . "]]></description>\n";
			$xml .= "</post>\n";
		}
		$xml .= "</posts>";
		mysql_close();
		
		header("Content-Type: application/xml; charset=UTF-8");
		echo $xml;
	}
	
	function save($id, $title, $description)
	{
		$this->dbConnect();
		$query = "SELECT * FROM $this->table WHERE id='$id'";
		$result = mysql_db_query (DB_NAME, $query, LINK);
		if (mysql_num_rows($result) > 0)
		{
			$query = "UPDATE $this->table SET title='$title', description='$description', date=NOW() WHERE id='$id'";
			$result = mysql_db_query (DB_NAME, $query, LINK);
			//$result = @mysql_query($query);
		}
		else
		{
			$query = "INSERT INTO $this->table (title, description, date) VALUES ('$title', '$description', NOW())";
			$result = mysql_db_query (DB_NAME, $query, LINK);
			//$result = @mysql_query($query);
		}
		mysql_close();
		$this->get();
	}
	
	function delete($id)
	{
		$this->dbConnect();
		$query = "DELETE FROM $this->table WHERE id='$id'";
		$result = mysql_db_query (DB_NAME, $query, LINK);
		mysql_close();
		$this->get();
	}

}

?>

Posted: Fri Oct 20, 2006 11:38 am
by RobertGonzalez
And the entire page that calls it?

Posted: Fri Oct 20, 2006 3:02 pm
by intergroove
This is the post.php:

Code: Select all

<? 
        $method=$_GET['method']; $id=$_GET['id']; $title=$_GET['title']; $description=$_GET['description']; 
        require_once("../classes/Post.class.php"); 
        $post = new Post(); 
        $post->$method($id, $title, $description); 
?>
Called by:
http://www.kickinmusic.com/sns/ajax/ser ... method=get

This demonstrates the problem ie. the extra lines.

My respondeXML is Null because of this.

Posted: Fri Oct 20, 2006 3:11 pm
by feyd
Why is this in a new thread?

Posted: Fri Oct 20, 2006 3:15 pm
by intergroove
Sorry I solved the first problem (first post) on the Windows 2003 Server. This - though the same problem is on a different server with the working code from the first. Hope you followed that.
I'm trying to focus entirely on the new prob. I'm really hoping someone can shed some light soon.

Posted: Mon Oct 23, 2006 9:34 am
by intergroove
Still hoping for some help on this one.

Somebody mentioned that a server plug-in could be the cause of the extra line appearing. It would be handy if someone could elaborate on this.

I'm using a work-around at the moment so I can progress and show my paymasters that I am getting somewhere - I'm using responseText and parsing from there, but I would much rather use responseXML - but with the extra lines appearing this is impossible.

Posted: Mon Oct 23, 2006 9:49 am
by volka
call <?php phpinfo(); ?> to find the approriate php.ini
open this php.ini in a texteditor
Find auto_prepend_file
If this line has a leading ; replace the whole line by auto_prepend_file=c:/autoprep_test.php
If there is no such line in the php.ini add auto_prepend_file=c:/autoprep_test.php
If there is such a line without a leading ; post the line and stop.
Save the file.
Create a new file c:\autoprep_test.php with the contents

Code: Select all

<?php echo __FILE__, ' ', __LINE__; flush(); ?>
Save the file.
Restart the webserver.
What does your script print now?