I try to get the contents of this json URL: http://www.der-postillion.de/ticker/newsticker2.php
Problem seems to be that the contents of "text" have Unicode within.
Everytime I try to get the json_decode, it fails with NULL...never had that issue before. always pulling json that way:
$news_url_postillion = 'http://www.der-postillion.de/ticker/newsticker2.php';
$file = file_get_contents($news_url_postillion, false, $context);
$data = json_decode($file, TRUE);
//debug
print_r(array($data));
$news_text = $data['tickers'];
//test
echo $news_text->text[0]; //echo first text element for test
foreach($news_text as $news){
$news_text_output = $news->{'text'};
echo 'Text:' . echo $news_text_output; . '<br>';
}
Anybody any idea what is wrong here? tries to get encoding working for hours with things like:
header("Content-Type: text/json; charset=utf-8");
or
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Content: type=application/json\r\n" .
"Content-Type: text/html; charset=utf-8"
)
);
$context = stream_context_create($opts);
but no luck :(
Thanks for your help!
Solution:
the json source has some unwanted elements in it, like the BOM character at json start. I could not influence the source json, so the solution walkingRed provided put me on the right track. Only the utf8_decode was needed due to his code is only for english language without special characters.
My working code solution for parsing and output the json is:
<?php
// Postillion Newsticker Parser
$news_url_postillion = 'http://www.der-postillion.de/ticker/newsticker2.php';
$json_newsDataPostillion = file_get_contents($news_url_postillion);
// Fix the strange json source BOM stuff
$obj_newsDataPostillion = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_newsDataPostillion), true);
//DEBUG
//print_r($result);
foreach($obj_newsDataPostillion['tickers'] as $newsDataPostillion){
$newsDataPostillion_text = utf8_decode($newsDataPostillion['text']);
echo 'Text:' . $newsDataPostillion_text . '<br>';
};
?>