如何在php中回显xml文件 [英] How to echo xml file in php

查看:86
本文介绍了如何在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');

这篇关于如何在php中回显xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆