2014-10-29 63 views
-2

我写这个php脚本来解析一些XML文档。php simplexml加载远程xml返回布尔(false)

$xml = simplexml_load_file('http://www1.cbs.gov.il/xml/indices_heb_all.xml'); 
var_dump($xml); 

当我运行此脚本,它的回声“布尔(假)” 我做错了吗? 谢谢!

+1

'simplexml_load_file'返回'假'当它无法加载文档时。我认为你没有正确使用它;你不需要'rawurlencode'的URL? http://php.net/manual/en/function.simplexml-load-file.php – 2014-10-29 16:09:58

+0

我认为你的xml编码不是utf8。它可能试图渲染特殊字符,从而无法将其加载为对象。 – 2014-10-29 16:12:08

+0

是你的文档编码是iso-8859-8-i,这会导致分析器错误:Unsupported encoding iso-8859-8-i' – 2014-10-29 16:17:02

回答

0

这里是会解决你的问题:

$xml = simplexml_load_string(str_replace('iso-8859-8-i', 'iso-8859-8', file_get_contents('http://www1.cbs.gov.il/xml/indices_heb_all.xml'))); 
var_dump($xml); 

但是编码必须设置为UTF-8的文本能够正确显示:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
</head> 
<body> 
<?php 
$xml = simplexml_load_string(str_replace('iso-8859-8-i', 'iso-8859-8', file_get_contents('http://www1.cbs.gov.il/xml/indices_heb_all.xml'))); 
var_dump($xml); 
?> 
</body> 
</html>