php file_get_contents无法读取xml服务

时间:2010-12-19 10:58:18

标签: php xml file-get-contents

我想从url读取并解析xml数据。我的网址是:“http://xml.gamebookers.com/sports/bandy.xml”。我可以从浏览器访问xml数据。但是,当我试图通过使用它来读它不起作用。这样的错误:

警告:file_get_contents(http://xml.gamebookers.com/sports/bandy.xml):无法打开流:

中的连接超时

我该如何解决这个问题呢?对此有任何意见吗?

提前致谢..

2 个答案:

答案 0 :(得分:2)

请参阅here获取答案:

  

此错误最有可能与之相关   途中有太多(HTTP)重定向   从您的脚本到您想要的文件   打开。 default redirection limit should be 20Streams。作为20   重定向是很多可能的   是文件名本身的一些错误   (导致例如网络服务器)   另一端做一些   spellcheck-redirects)或其他   服务器配置错误或有   一些安全措施到位或......

     

如果你觉得有必要延长20   重定向你可以使用流   上下文。

$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);
// ...

请参阅:

答案 1 :(得分:1)

尝试使用代码段:  $ request_url ='http://xml.gamebookers.com/sports/bandy.xml';

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

/ * 然后只解析xml的子节点。 例如 * /

foreach($ xml-> children()为$ child)       {        echo $ child-> getName()。“:”。$ child。“
”;       }

希望这个帮助

PS:打开你的PHP.INI并寻找      allow_url_fopen = On //确保它为ON