如何在php中回显xml文件
问题描述:
如何在php中将xml文件打印到屏幕上?
How to print an xml file to the screen in php?
这不起作用:
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, 'http://rss.news.yahoo.com/rss/topstories');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
$xml = simplexml_load_string($result);
echo $xml;
有一个简单的解决方案吗?也许没有SimpleXML?
Is there a simple solution? Maybe without SimpleXML?
答
由于您可以通过file_get_contents()从URL获取内容,然后回显它,甚至可以使用readfile()直接读取它
You can get the contents from an URL via file_get_contents() and then echo it, or even read it directly using readfile()
$file = file_get_contents('http://example.com/rss');
echo $file;
或
readfile('http://example.com/rss');
不过,在输出任何内容之前,请不要忘记设置正确的MIME类型.
Don't forget to set the correct MIME type before outputing anything, though.
header('Content-type: text/xml');