4

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>';
};
?>
Community
  • 1
  • 1
MonkeyKingFlo
  • 143
  • 2
  • 7

2 Answers2

2

I made some search and get this:

$result = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $file), true);

Original post

Community
  • 1
  • 1
Tomasz Ferfecki
  • 1,263
  • 14
  • 22
  • that put me on the right track! thank you very much. It was not direct usable, because I have special chars in my strings due to german language, and your solution kills the special chars, but I fixed that with the above code. Most important your solution provided me the needed valid array output :-) – MonkeyKingFlo Mar 17 '16 at 20:46
  • @MonkeyKingFlo during search I find out that german characters are very tricky in json encoding/decoding. Something new for You something new for me :) – Tomasz Ferfecki Mar 18 '16 at 10:11
0

BOM! There is a BOM character at the beginning of the document which you linked and you need to remove it before you try to decode its content.

You can see it e.g. if you would download that json with wget and display it with less.

michaJlS
  • 2,465
  • 1
  • 16
  • 22
  • yes, thank you, thats right! I overlooked that...your answer and walkingRed's answer put me on the right track! So thank you! – MonkeyKingFlo Mar 17 '16 at 20:45