views:

207

answers:

4

Hello, my question is how I can send clean xml from this class. It is simplified, so there is the least off trouble that could have been caused by other things.

The problem now is that the error says that the xml declaration should occur at the top off the document.

What is the right way to avoid this kind off errors? Is there some php function that would do this for me?

I have to mention that I am doing this the REST way. So, no hassle with soap servers, xml-rpc servers.

The first part is the instantiation off the class in the index.php.

The class is in the included file.

    <?php
include 'controller/' . 'gpsController.php';
$gps=new gpsController('eenwaarde');
$gps->setgpsloc();
exit();
?> 


<?php 
class gpsController  {   
 // extends controller
  public $url;   



  function __construct($url) {   
    $this->url = $url; 
    }  


public function setgpsloc() { 
$gebruikersnaam="kabouter";
$status_code = 2;
header('Content-type: text/xml');
     $output= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
     $output.= "<response>\n";
     $output.= "\t<status>$status_code</status>\n";
     $output.= "\t<fout>$gebruikersnaam</fout>\n";
     $output.= "</response>";
     echo trim($output);
}


public function index() {
}

}   

?>

Thanks, Richard

A: 
Jasper
found it, trailing space behind the ?> tag
A: 

The given code looks strange. I think the "<?php" lines needs to be moved up as the first line. You should also check whether any of your included files accidently perform some output, e.g. by having a space or line break in front of their first "<?php" or after their last "?>".

Apart from that, you could use an XML library, but this will only ensure your XML code is well-formed. It is most important to have clear, lucid XML creation code. This is best done with a template library such as PHP itself. :-)

<?php header('Content-type: text/xml') ?>
<?xml version="1.0" encoding="utf-8"?>
<response>
    <status><?php echo htmlspecialchars($status_code) ?></status>
    <fout><?php echo htmlspecialchars($gebruikersnaam) ?></fout>
</response>
vog
I edited my question.//@vog, you where completely right.//It was space behind the last ?>. //I am sorry, this is the second time I fall for this output mistake. I should remember this one. – Richard 25 mins ago
A: 

looks like you are echoing out a whole lot of white space before the header is being sent.

Firstly your first line has white space before the "<?php":

    <?php

You are also doing an include before you run your first function.. if that included file has white space before the "<?php" or after the "?>" that will be sent back to the browser before the function is run... As soon as you start sending a response, and the header has not been set yet, then the default header will be sent by php.. and when you go to set your header in the function you will get the error that you are describing.

Bingy
A: 

You might want to consider leaving out the closing ?> tag in all your scripts. PHP will automatically add it. This way you will never have the issue of trailing whitespace behind the ?> tag.

Shaunak Kashyap