0

我的问题实际上是

如果我有一个类中的大部分逻辑,我将使用什么样的结构来发送 xml 作为 REST 服务的一部分。如果很明显正在请求服务,我会在我的 php 索引页面顶部调用/包含该类。

有人向我提到类方法不应该输出任何东西。那么我应该在哪里输出xml。课外?

另外我有一个问题,接收端说声明应该从文档的开头开始。

接收端在文档中只有这两行。我还没有代码来处理它,但这已经给出了一个错误。

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

如您所见,我从这个类发送了 xml,所以也许这里出了点问题,有白线或其他东西。无论如何,我需要一些建议如何避免声明错误以及如果我不应该在方法中发送 xml?先将它存储在类变量中然后也许?

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



}   

?> 

这就是我检测是否请求服务并调用类的方式

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

?>

这是接收端得到的实际错误

PHP 警告:simplexml_load_string() [function.simplexml-load-string]:实体:第 4 行:解析器错误:仅在 D:\Domains\tsa.nl\wwwroot\index.php 中的文档开头允许 XML 声明第 4 行 PHP 警告:simplexml_load_string() [function.simplexml-load-string]:在 D:\Domains\tsa.nl\wwwroot\index.php 第 4 行 PHP 警告:simplexml_load_string() [function.simplexml-load-string ]: ^ 在 D:\Domains\tsa.nl\wwwroot\index.php 第 4 行

4

3 回答 3

1

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! :)

于 2009-11-10T16:51:58.487 回答
1

与“ http://www.woonbel.nl/gps/setgpsloc ”一样,XML 声明前有空格。我建议你修剪(http://php.net/trim)你的输出。

    //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);

如果您的代码中有其他内容可能正在发送这些输出,您可以在缓冲区中获取它:

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

    //In the end of your script
    $output = ob_get_clean();
    echo trim($output);
于 2009-07-24T02:47:09.190 回答
0

好吧,快速回答是您在文档中的“<?xml”之前有 4 个空行,这导致了错误。您需要仔细检查您的代码并找出正在做的事情(尽管我无法诚实地弄清楚这是如何发生的,因为如果您在标头之前发送任何输出,理论上标头会引发错误)。您发布的代码肯定不会在 xml 声明之前输出额外的 4 行,因此问题出在代码的其他地方:)

[更新]希望我能发表评论,但关于提供的其他答案,输出缓冲和修剪肯定会解决问题,但更好的解决方案是找出那些额外的 4 行空白来自哪里(无意侮辱,但修整输出缓冲区是一种创可贴的解决方案)

于 2009-07-24T02:46:07.490 回答