Getting hebrew json to php file

1.2k views Asked by At

I'm trying to parse this json file in php: http://www.oref.org.il/WarningMessages/alerts.json

This file contains Hebrew letters which cause encoding problems.

My script:

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

It just won't display anything. I just get a blank page. But if I do echo $content; it shows the json file perfectly.

Json file example:

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

I've been reading few other similar problems and solutions but none of them helped fixing this problem. I've been trying to use mb_detect_encoding and iconv but it didn't help.

Thank you!

2

There are 2 answers

1
umka On BEST ANSWER

The file content you're getting is in UTF-16 charset. You have to convert it:

$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);
5
VolenD On

You can try the following iconv:

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

then json_decode works properly and returns:

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

)