2010-12-19 52 views
0

我想读取和解析来自url的xml数据。我的网址是:“http://xml.gamebookers.com/sports/bandy.xml”。我可以访问xml数据浏览器。但是,当我试图读取它通过使用PHP它does not work.It错误是这样的:php file_get_contents无法读取xml服务

警告:file_get_contents(http://xml.gamebookers.com/sports/bandy.xml):未能打开流:连接超时在

我该如何解决这个问题?对此有何评论?

在此先感谢..

+0

我建议对子级执行以下操作:确保文件存在于你试图打开并获取内容的URL。确保allow_url_fopen处于ON状态。 – Andreas 2010-12-19 11:12:44

+0

网址是可用的,因为我可以从浏览器访问它,allow_url_fopen是on.I尝试另一种解决方案是这样的:我使用file_get_contents与流参数(代理),我使用simple_ml_load返回值file_get_contents.However返回值file_get_contents是字符串,所以simplexml_load有错误,因为字符串参数,而不是xml文件参数 – 2010-12-19 13:15:44

回答

2

请参阅here一个答案:

This error is most likely connected to too many (HTTP) redirects on the way from your script to the file you want to open. The default redirection limit should be 20 . As 20 redirects are quite alot there could be some error in the filename itself (causing e.g. the webserver on the other end to do some spellcheck-redirects) or the other server is misconfigured or there are some security measures in place or...

If you feel the need to extend the 20 redirects you could use a stream context.

$context = array(
    'http'=>array('max_redirects' => 99) 
); 
$context = stream_context_create($context); 
// hand over the context to fopen() 
$data = file_get_contents('http://xml.gamebookers.com/sports/bandy.xml', false, $context); 
// ... 

请参阅:

+0

谢谢Stefan我通过使用curl与代理选项解决了它。我得到了xml数据。但它非常慢。是否有任何方法将参数传递给xml url?例如:“http: //xml.gamebookers.com/sports/bandy.xml?name=footbal“?我想要使用这样的函数来过滤result.I不需要全部结果 – 2010-12-19 15:21:09

+0

否 - 至少不是如果XML文件真的是XML文件。如果它是某种REST端点(这意味着该URL后面有一个脚本运行),则可以将其他参数传递给该URL。如果它只是一个文件,它将作为一个文件下载。 – 2010-12-19 16:21:40

1

尝试片断: $ request_url = 'http://xml.gamebookers.com/sports/bandy.xml';

$ xml = simplexml_load_file($ request_url)或die(“feed not loading”);

/* 然后只是解析你的XML的子节点。 。 例如 */

的foreach($ XML->儿童()为$子) { 回声$儿童安全>的getName() “: ” $儿童 “
”; }

希望这有助于

PS:打开你的php.ini,查找 了allow_url_fopen =开//确保它是ON

+0

嗨dbwebtek感谢您的关心,但我得到以下错误:警告:simplexml_load_file(http://xml.gamebookers.com/sports/bandy.xml):未能打开流:已达到重定向限制,在第13行的/var/www/fbDemo/rss.php中放弃警告:simplexml_load_file():I/O警告:未能加载外部实体“http://xml.gamebookers.com /sports/bandy.xml“在/var/www/fbDemo/rss.php在线13饲料不加载 – 2010-12-19 12:10:23

+0

顺便说一句我的应用程序是在localhost,我使用的是proxy.Without代理我无法到达该XML服务浏览器 – 2010-12-19 12:13:13

+0

我用这样的流(因为我使用代理):$ default_opts = array( 'http'=> array( 'method'=>“GET”, 'header'=>“Accept-language: en \ r \ n“。 “Cookie:foo = bar”, 'proxy'=>“tcp:// myProxyIp:myPort” ) );然后我用流参数fie_get_contents()函数,它的工作。但有没有什么办法可以在simple_xml_load中使用流参数? – 2010-12-19 12:39:36