使用php从URL解析XML文件 [英] Parse XML file from url using php

查看:101
本文介绍了使用php从URL解析XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一台服务器解析xml文件.

I want to parse xml file from another server.

<?php

    $xml = simplexml_load_file('http://example_page.com/api/test.xml');

?>

仅当此文件与页面位于同一服务器上时,此代码才有效,但是我在另一台服务器上有xml文件.

And this code work only if this file is on this same server as page, but I have got xml file on another server.

网页警告:

Warning: simplexml_load_file(http://example_page.ugu.pl/api/test.xml) [function.simplexml-load-file]: failed to open stream: Connection refused in /virtual/z/y/example_page.ugu.pl/index.php on line 14

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://example_page.com/api/test.xml" in /virtual/z/y/example_page.ugu.ugu.pl/index.php on line 14

P.S. allow_url_fopen仍处于启用状态.

P.S. allow_url_fopen is still on.

推荐答案

似乎您正在访问的url中存在一些重定向问题,并且很可能simplexml_load_file()没有遵循重定向...

Seems like there is some redirection problem in the url you are accessing, and most probably simplexml_load_file() doesn't follow redirects...

因此解决方案是使用file_get_contents()cUrl ...

So the solution would be to use file_get_contents() or cUrl...

由于file_get_contents()更容易,我只显示...

As file_get_contents() is easier, I am showing that only...

代码必须类似于:

<?php

    $xml = simplexml_load_string(file_get_contents('http://example_page.ugu.pl/api/test.xml'));

?>

更多:

<?php

    $xml = simplexml_load_string(file_get_contents('http://jakdojade.pl/pages/api/outputs/schedules.xml'));

?>

^那也完全可行!

这篇关于使用php从URL解析XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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