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>';
};
?>
4
  • Did you check json_last_error ? php.net/manual/en/function.json-last-error.php Commented Mar 17, 2016 at 17:59
  • Have you tried running utf8_decode() before trying to parse the JSON? php.net/manual/en/function.utf8-decode.php Commented Mar 17, 2016 at 18:04
  • Where is your $context = stream_context_create()?? Commented Mar 17, 2016 at 18:10
  • thanks for the guesses, I added my solution. Answer 1 from walkingRed put me on the right track. Commented Mar 17, 2016 at 21:01

2 Answers 2

2

I made some search and get this:

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

Original post

Sign up to request clarification or add additional context in comments.

2 Comments

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 during search I find out that german characters are very tricky in json encoding/decoding. Something new for You something new for me :)
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.

1 Comment

yes, thank you, thats right! I overlooked that...your answer and walkingRed's answer put me on the right track! So thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.