问题!em使用php发送xml(声明xml)
我的问题实际上是 如果我在一个类中有大部分的逻辑,我会使用什么样的结构来发送xml作为REST服务的一部分。如果发现有服务被请求,我会在php索引页面的顶部调用/包含该类 有人向我提到类方法不应该输出任何东西。 那么我应该在哪里输出xml呢。课外活动 我还有一个问题,就是接收端说申报应该从文件的开头开始 接收端在文档中只有这两行。 我还没有代码来处理它,但这已经给出了一个错误问题!em使用php发送xml(声明xml),php,xml,parsing,rest,simplexml,Php,Xml,Parsing,Rest,Simplexml,我的问题实际上是 如果我在一个类中有大部分的逻辑,我会使用什么样的结构来发送xml作为REST服务的一部分。如果发现有服务被请求,我会在php索引页面的顶部调用/包含该类 有人向我提到类方法不应该输出任何东西。 那么我应该在哪里输出xml呢。课外活动 我还有一个问题,就是接收端说申报应该从文件的开头开始 接收端在文档中只有这两行。 我还没有代码来处理它,但这已经给出了一个错误 <?php $url='http://www.woonbel.nl/gps/setgpsloc'; $xml =s
<?php
$url='http://www.woonbel.nl/gps/setgpsloc';
$xml =simplexml_load_string(file_get_contents($url));
?>
正如您所看到的,我从这个类中发送了xml,所以这里可能出了问题,
有白线或其他东西的东西。
无论如何,我需要一些建议,如何避免声明错误,如果不应该在方法中发送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\u load\u string()[function.simplexml load string]:实体:第4行:解析器错误:仅在第4行的D:\Domains\tsa.nl\wwwroot\index.PHP中的文档开头允许XML声明PHP警告:simplexml\u load\u string()[function.simplexml load string]:在第4行的D:\Domains\tsa.nl\wwwroot\index.php中,php警告:simplexml\u load\u string()[function.simplexml load string]:^在第4行的D:\Domains\tsa.nl\wwwroot\index.php中,中,快速的答案是“前面有4行空行,在XML声明之前有空格。 我建议您调整()输出
//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);
我们的问题很有可能与我的问题相同。解决方案非常简单,正如预期的那样。我网站上的每个页面都需要一个文件,其中包含我为解析和管理网站而创建的一些类。该文件在关闭php标记
?>
之后有一个空格“”
由于该空格不在脚本中,因此在调用require时,默认情况下它包含在文档中。因此,请检查您包含/需要的所有文件的结尾。我的页面正在声明
,但它引用的是什么空格。我将尝试您的方法,也许它会神奇地工作。您只是错过了连接部分str的点ings。你在这里发布的代码中没有打印这些空格,但我相信使用缓冲区可以让它工作。我会编辑点,呵呵。谢谢,我希望可以,但我不知道它是从哪里来的,这很愚蠢。难道没有什么工具可以检测它吗?我不知道任何特定的工具用于这种情况,但你可以尝试调试你的代码的部分,直到你在这里找到它。我同意你,修剪它是一个创可贴解决方案,找出这个空间来自哪里是最好的解决方案。它只是一个“快速修复”。“heheI编辑了我的问题,修剪也不起作用。那么其他人看起来还好吧?我将添加取消类代码花了数小时寻找解决方案,正是这个问题:在require\u once
文件中的?>
标记后有一个空格。非常感谢。
//In the beginning of your script
ob_start();
//In the end of your script
$output = ob_get_clean();
echo trim($output);