Masayuki Tonoki
Masayuki Tonoki

Reputation: 167

XML Error when I try to do simplexml_load_file

I am trying to turn a XML data from another website into arrays in my program. This is what I have written so far:

<?php
$rss = 'http://headlines.yahoo.co.jp/rss/asahik-dom.xml';
$xml = simplexml_load_file($rss);
var_dump($xml);
?>

However, when I try to load the php page, it comes up with this error:

Warning: simplexml_load_file(http://headlines.yahoo.co.jp/rss/asahik-dom.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! in /home/www2/it32.lady2.itall.co.jp/www/yxml.php on line 11

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://headlines.yahoo.co.jp/rss/asahik-dom.xml" in /home/www2/it32.lady2.itall.co.jp/www/yxml.php on line 11 bool(false)

FYI the $xml = simplexml_load_file($rss); is line 11.

Which part of my code has gone wrong? Please help.

Upvotes: 0

Views: 1243

Answers (1)

Hassaan
Hassaan

Reputation: 7662

Please try file_get_contents() to load file and then use SimpleXMLElement to parse it.

Try

$rss = file_get_contents('http://headlines.yahoo.co.jp/rss/asahik-dom.xml');
$xml = new SimpleXMLElement($rss);
print_r($xml);

NOTE allow_url_fopen must be enabled in php.ini

Upvotes: 1

Related Questions