views:

1046

answers:

3

Hello, my question is actually

What kind off structure would I use to send xml as part off a REST service if I have most off the logic in a class. I call/include the class at the top off my php index page if it becomes clear a service is being requested.

Someone mentioned to me that a class method should not output anything. Then where should I output the xml. Outside the class?

Also I have a problem that the receiving end is saying that the declaration should start at the start off the document.

The receiving end only has these two lines in the document. I do not have code to process it yet, but this already gives an error.

<?php
$url='http://www.woonbel.nl/gps/setgpsloc';
$xml =simplexml_load_string(file_get_contents($url));  
?>

I send the xml from this class as you can see, so maybe here something go's wrong, something with white lines or something else. Anyway, I need some advice how to avoid the declaration error and how to send xml if I am not supposed to do it in a method?store it in a class variable first then maybe?

<?php 
class gps {   

  public $url;   
  public $test;     

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

  function invoke($methode_naam) {   
  switch($methode_naam){

  case "test":
  $this->setgpsloc();

  break;
   case "setgpsloc":
  header('Content-type: text/xml');  


$status_code = 2;
     $output= "<?xml version=\"1.0\"encoding=\"utf-8\"?>\n";
     $output.= "<response>\n";
     $output.= "\t<status>$status_code</status>\n";
     $output.= "\t<fout>Geen</fout>\n";
     $output.= "</response>";
     echo trim($output);     


  }


  }//EINDE invoke 



}   

?>

This is how I detect if a service is requested and call the class

<?php
//WEBSERVICE SECTIE
$url = $_GET['url']; 
$parts = split('/', $url); // Opslaan van delen van de aangevraagde url in $parts
$cparts=count($parts);   
//if($cparts>=2){
$controller = $parts[0];
$wslijst=array("gps","gebruikers");
if(in_array($controller,$wslijst)){
    include $controller .".php";
    $clr = new $controller("test");
    $clr->invoke($parts[1]);
    exit();  
    }
//other code underneath for displaying normal page

?>

This is the actual error the receiving end get's

PHP Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 4: parser error : XML declaration allowed only at the start of the document in D:\Domains\tsa.nl\wwwroot\index.php on line 4 PHP Warning: simplexml_load_string() [function.simplexml-load-string]: in D:\Domains\tsa.nl\wwwroot\index.php on line 4 PHP Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in D:\Domains\tsa.nl\wwwroot\index.php on line 4

thanks, Richard

A: 

Well, the quick answer is that you have 4 blank lines before your "<?xml" in the document, which is causing the error. You'll need to go through your code and find out what's doing that (although I can't honestly figure out how that's happening, because the header would theoretically throw an error if you sent any output before it). The code you've posted definitely doesn't output the extra 4 lines before the xml declaration, so the issue's elsewhere in your code :)

[UPDATE] Wish I could comment, but in regards to the other answer supplied, output buffering and trimming will certainly solve the problem, but the better solution would be to figure out where those extra 4 lines of whitespace are coming from (no insult intended, but trimming the output buffer is a bit of a band-aid solution)

Ian Selby
I agree with you, trimming it is a band-aid solution, figuring out where this spaces come from is the best solution. It was just a "quick-fix" hehe
Nathan
I edited my question, trimming diddn't work either. So the rest off the class looks ok? I will add the calling off the class code
Richard
@Nathan, nothing wrong with quick-fixes ;)
Ian Selby
A: 

Hi Richard.

As in "http://www.woonbel.nl/gps/setgpsloc", you have blank spaces before the XML declaration. I recommend you trim (http://php.net/trim) your output.

    //Edit, you forgot a space between \" and encoding bellow
    $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
                  //-----------------^
    $output .= "<response>\n";
    $output .= "\t<status>$status_code</status>\n";
    $output .= "\t<fout>Geen</fout>\n";
    $output .= "</response>";
    echo trim($output);

If there is something else in your code that maybe are sending these outputs, you can get it on a buffer:

    //In the beginning of your script
    ob_start();

    //In the end of your script
    $output = ob_get_clean();
    echo trim($output);
Nathan
But what white spaces is it reffering to.I will try your way, maybe it will magically work. You only missed the dottes to connect partial strings.
Richard
Nothing in the code you posted here are printing these spaces, but I believe using the buffer you can get it working. I'll edit the dots, hehe.
Nathan
thanks, I hope that works, but it is stupid that I can't figure out where it is coming from.
Richard
issn't there some tool to detect it?
Richard
I don't know any specific tool for that situation, but you can try debugging portions of your code 'till you find here it came from.
Nathan
I try'd just sending the xml before any class declaration. The include will just echo the output.The I see this: Entity: line 1: parser error : Blank needed here in D:\Domains\tsa.nl\wwwroot\index.php on line 4//////It is a little different.Maybe the file_get_contents is doing something wrong?
Richard
Richard, the actual error is about a missing space between the version propriety and encoding: "<?xml version=\"1.0\"(You need add space here)encoding=\"utf-8\"?>\n".
Nathan
oh, okI can't test it now. I will do that some time later.You will here from me
Richard
thank you, that was it.
Richard
A: 

Hi,

There is a good chance that our problem is the same I was having. The solution was really simple, as expected. Every page on my site require__once a file with some classes I made to parse and manage the site. This file had a space ' ' right after the closing php tag ?>

As that space is outside of the script, it was included on the document by default when require was called. So, check the end of all the files you are including/requiring. My pages are declaring the <?xml without problems now. IT HAS TO BE THE FIRST THING!

Here is my validation: W3C VALIDATOR TO MY PAGE
You can find the site link there as well (it is my father's band site.. lol)

So, check any space or newline OUTSIDE THE <?PHP ?> that is probably where it is coming from (using include or require will parse that outside the script, on the html document (and before you parse or execute anything else, that came in when you included the file, so it is 'before' everything you are doing, trimming is out of your power and scope on this case).

PS: If you check the Validator, and then go to the page and check the source code, you will see the <?xml declaration, My pages are 100% parsed (No PHP inside HTML).

Hope this helps... Cheers! :)