我在名为abc.php的文件中有这个,它将返回一个有效的xml文档,而不是在末尾和开头显示-string-标签
header('Content-type: application/xml');
$xml = file_get_contents("http://www.xxx.asmx/test?id=1"); //External web service
$xmlstr = simplexml_load_string($xml);
echo $xmlstr;
我想使用abc.php的有效xml数据,提取某些数据,将其存储在我的数据库中,并定期检查其他服务器的输出,我尝试过:
ob_start();
include 'abc.php';
$result = ob_get_clean()
以及this:
$xml = file_get_contents("abc.php");
$xmlstr = simplexml_load_string($xml);
没有成功,有什么建议吗?
确保您也输出MIME类型,否则服务器将向它提供文本/html,这将是错误的。把这个函数
header("Content-type: application/xml");
file_get_contents("abc.php")
将返回文件"abc.php"的内容;它不会执行PHP代码。带输出缓冲技巧的include
应该大致正确,但我不确定为什么要这样做,所以不值得研究为什么会失败。
如果您可以访问abc.php
中的代码,那么只需将其转换为PHP函数,该函数返回处理后的XML:
function get_the_actual_xml()
{
$xml = file_get_contents("http://www.xxx.asmx/test?id=1"); // External web service
$xml_obj = simplexml_load_string($xml); // Load into SimpleXML object
return (string)$xml_obj; // Convert contents back to a string
}
如果由于某种原因您的两个PHP文件需要位于不同的服务器上,您将需要将URL引用到abc.php
,而不仅仅是它在磁盘上的位置。这样,PHP代码将被执行,您将得到的是echo
语句的结果。如果您的服务器启用了allow_url_fopen
设置,则只需输入$remotely_processed_content = file_get_contents('http://sanjosecostarica.org/test/abc.php')
最后我不能得到"abc.php"的结果,但只有内容,我尝试了一个不同的方法成功:
$xml = file_get_contents("http://www.xxx.asmx/test?id=1"); //External web service
$xmlstr = simplexml_load_string($xml);
$xmlok = <<<XML
$xmlstr
XML;
$xml = simplexml_load_string($xmlok);