获取希伯来文 json 到 php 文件

Getting hebrew json to php file

我正在尝试解析 php 中的这个 json 文件:http://www.oref.org.il/WarningMessages/alerts.json

此文件包含导致编码问题的希伯来字母。

我的脚本:

$content = file_get_contents('http://www.oref.org.il/WarningMessages/alerts.json'); 
$json = json_decode($content);
echo $json->id;

就是不显示任何东西。我只是得到一个空白页。但是如果我这样做 echo $content; 它会完美地显示 json 文件。

Json 文件示例:

{ 
"id" : "1434292591050",
"title" : "פיקוד העורף התרעה  באזור ",
"data" : []
}

我一直在阅读其他一些类似的问题和解决方案,但其中 none 帮助解决了这个问题。我一直在尝试使用 mb_detect_encodingiconv 但它没有帮助。

谢谢!

您可以尝试以下方法iconv

$content = iconv('utf-16', 'utf-8', $content);

然后 json_decode 正常工作并且 returns:

stdClass Object
(
    [id] => 1434292591050
    [title] => פיקוד העורף התרעה  באזור 
    [data] => Array
        (
        )

)

您获取的文件内容是 UTF-16 字符集。你必须转换它:

$content = file_get_contents('http://www.oref.org.il/WarningMessages/alerts.json'); 
$content=iconv("UTF-16", "UTF-8", $content);
$json = json_decode($content,true);
print_r($json);