XML Parsing Error: xml declaration not at start of external entity

Posted on 2007-11-22
Last Modified: 2008-02-01
im building my own trackback class for my personal blog.

when i try calling my trackback URL like this: http://localhost/my-blog/this-is-a-test-entry/trackback/  i get the following error:

XML Parsing Error: xml declaration not at start of external entity
Location: http://localhost/my-blog/this-is-a-test-entry/trackback/
Line Number 1, Column 2: <?xml version="1.0" encoding="UTF-8"?>

when i look at the source code, i dont have any whitespaces.  i have checked every line of my code over and over again incase i missed one hiding...

i searched on the web and read that a 8-bit character can be inserted infront of the XML declaration tag but i dont know how it could have?

if i create another example like below it works fine...

function test()
      $xmlWrite = '';
      $xmlWrite .= "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
      $xmlWrite .= "<test>";
      $xmlWrite .= "<success>works fine...</success>";
      $xmlWrite .= "</test>";
      return $xmlWrite;

header("Content-type: text/xml");
echo test();

please see my original code below.  I know most of this is PHP so ive included this question in the PHP zone too...
trackback.php - the page that is called when user requests :


header("Content-type: text/xml");

$host = trim($_SERVER['HTTP_HOST']);

if(substr($host,0,4) == 'www.')


	$host = substr($host,4);


$now = date('Y-m-d G:i:s');

$ip = trim($_SERVER['REMOTE_ADDR']);

$agent = trim($_SERVER['HTTP_USER_AGENT']);

$domain = 'http://www.'.trim($host).'/';


$trackback = new Trackback('','','UTF-8');

global $response, $comment;

$tb_id = trim($HTTP_GET_VARS['id']);

$tb_title = trim($HTTP_POST_VARS['title']);

$tb_blog_name = trim($HTTP_POST_VARS['blog_name']);

$tb_url = trim($HTTP_POST_VARS['url']);

$tb_content = substr($HTTP_POST_VARS['excerpt'],0,2000);

$permalink = $domain;



	$err_response = 'XML-RPC server accepts POST requests only.';


// do a few checks to see if trackback is spam or already in table etc etc



	echo trim($trackback->recieve(false,$err_response));




	echo trim($trackback->recieve(true,''));



class.trackback.php recieve function:

 function recieve($success = false, $err_response = "") 


        if (!$success && empty($err_response))


            $err_response = "An error occured while tring to log your trackback...";



		$return = '<?xml version="1.0" encoding="utf-8"?>'."\n";

        $return .= '<response>'."\n";


		if ($success) 


            $return .= '    <error>0</error>'."\n";




            $return .= '    <error>1</error>'."\n";

            $return .= '    <message>'.$this->xml_safe($err_response).'</message>'."\n";



        $return .= '</response>';

        return $return;


Open in new window

Question by:ellandrd
  • 2

Accepted Solution

nitinsawhney earned 500 total points
ID: 20333673

Looks like a header conflict in your code.

Just remove header("Content-type: text/xml"); from your code and it should do the trick.
If it doesn't work then add another "\n" to following line in the function receive
 $return = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";

LVL 16

Author Comment

ID: 20333686
ok after removing the header i dont get the above error,but if i view source i still get that whitespace or 8-bit char shown up...
 <?xml version="1.0" encoding="UTF-8"?>



    <message>You didnt post an id to apply the trackback to...</message>


Open in new window

LVL 16

Author Comment

ID: 20335875
I completely rewrote the whole class and now it works fine this time...

thanks for the time and help


Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now