PHP produced XML giving extra lines at start of doc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

PHP produced XML giving extra lines at start of doc

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post 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
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Dude, now it looks like two extra lines. Can you post the entire code for the page that produces that?
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post 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();
	}

}

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

And the entire page that calls it?
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why is this in a new thread?
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post 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.
intergroove
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 4:42 pm

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
Post Reply