gpt4 book ai didi

php - 如何在 PHP 中回显 XML 文件

转载 作者:IT王子 更新时间:2023-10-29 01:06:30 25 4
gpt4 key购买 nike

如何在 PHP 中将 XML 文件打印到屏幕上?

这不起作用:

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

最佳答案

感谢 PHP's wrappers,您可以像使用本地文件一样使用 HTTP URL

您可以通过 file_get_contents() 从 URL 中获取内容,然后回显它,甚至可以使用 readfile() 直接读取它

$file = file_get_contents('http://example.com/rss');
echo $file;

readfile('http://example.com/rss');

不过,不要忘记在输出任何内容之前设置正确的 MIME 类型。

header('Content-type: text/xml');

关于php - 如何在 PHP 中回显 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1199595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com