我正在尝试访问espncricinfo的RSS实时板球比分提要。
我的php代码是这样的
$xml = simplexml_load_file("http://static.cricinfo.com/rss/livescores.xml");
但它抛出这样的错误。
Warning: simplexml_load_file(http://static.cricinfo.com/rss/livescores.xml) [function.simplexml-load-file]: failed to open stream: Permission denied in /home/www/java8.in/txtWeb/demo.php on line 11
寻找解决方案。
你的代码实际上可以工作,但你需要设置allow_url_fopen
1
或ON
来实现你正在做的事情。
查看 PHP 手册here
了解如何执行此操作。
代码...
<?php
$xml = simplexml_load_file("http://static.cricinfo.com/rss/livescores.xml");
print_r($xml);
OUTPUT:
SimpleXMLElement Object ( [@attributes] => Array ( [version] => 2.0 ) [channel] => SimpleXMLElement Object ( [title] => Cricinfo Live Scores [ttl] => 2 [link] => http://www.cricinfo.com [description] => Latest scores from Cricinfo [copyright] => (c)Cricinfo [language] => en-gb [pubDate] => Tue, 04 Feb 2014 19:12:01 +0000 [item] => Array ( [0] => SimpleXMLElement Object ( [title] => Bangladesh v Sri Lanka 314/5 * [link] => http://www.cricinfo.com/ci/engine/match/690349.html?CMP=OTC-RSS [description] => Bangladesh v Sri Lanka 314/5 * [guid] => http://www.cricinfo.com/ci/engine/match/690349.html ) [1] => SimpleXMLElement Object ( [title] => South Africa 300/10 v South African Composite XI 24/3 * [link] => http://www.cricinfo.com/ci/engine/match/714095.html?CMP=OTC-RSS [description] => South Africa 300/10 v South African Composite XI 24/3 * [guid] => http://www.cricinfo.com/ci/engine/match/714095.html ) [2] => SimpleXMLElement Object ( [title] => Guyana 36/1 * v Windward Islands [link] => http://www.cricinfo.com/ci/engine/match/708797.html?CMP=OTC-RSS [description] => Guyana 36/1 * v Windward Islands [guid] => http://www.cricinfo.com/ci/engine/match/708797.html ) [3] => SimpleXMLElement Object ( [title] => Sri Lanka Cricket Women's XI v England Academy Women [link] => http://www.cricinfo.com/ci/engine/match/710579.html?CMP=OTC-RSS [description] => Sri Lanka Cricket Women's XI v England Academy Women [guid] => http://www.cricinfo.com/ci/engine/match/710579.html ) [4] => SimpleXMLElement Object ( [title] => Sydney Sixers v Perth Scorchers [link] => http://www.cricinfo.com/ci/engine/match/654097.html?CMP=OTC-RSS [description] => Sydney Sixers v Perth Scorchers [guid] => http://www.cricinfo.com/ci/engine/match/654097.html ) ) ) )
cURL 方式..
<?php
$url = "http://static.cricinfo.com/rss/livescores.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xmlresponse = curl_exec($ch);
$xml=simplexml_load_string($xmlresponse);
print_r($xml);
我发现一些共享主机提供商不支持访问外部 url,尽管allow_url_fopen
设置为 1。
问题的可能解决方案:
Shankar Damodaran在上面的答案中提到了两种解决方案
我发现的另一种解决方案或替代方法是ini_set
功能。 您可以在 php 文件中放置以下代码来修改php.ini
文件属性。
ini_set('allow_url_fopen', 'on');
如果您使用 SELinux 运行,您可能需要允许 Apache 访问网络
setsebool -P httpd_can_network_connect on