jimtour
jimtour

Reputation: 31

json_decode in php

I want to use json_decode to this json file but something is going wrong. It was working fine a few days ago but now returns NULL.

<?php 

$url = 'http://opendata.diavgeia.gov.gr/api/decisions?org=eot&output=json'; 


function works2($url) 
{ 
   $opts = array( 
     'http'=>array( 
       'method'=>"GET", 
       'header'=>"Connection:Keep-Alive\r\nAccept:*/*\r\n" 
     ) 
   ); 

   $context = stream_context_create($opts); 
   $retstr = file_get_contents($url,false,$context); 
   return $retstr; 
} 


var_dump(json_decode(works2($url)));
?>

Could anyone help me?

Upvotes: 0

Views: 289

Answers (3)

skyrail
skyrail

Reputation: 149

works2 result is ok. Maybe the json encoded content is too big or (probably) contains unwanted characters or a malformed sequence. Take a look to default opendata output. It is xml format, but far long to download.

Upvotes: 0

Roopak Venkatakrishnan
Roopak Venkatakrishnan

Reputation: 563

I used a JSON validator and the url seems to provide an INVALID JSON

try using jsonList to validate the URL here http://jsonlint.com/

I get

 Parse error on line 822:
 ...         "subject": "Ματαίωση του πρόχει
 -----------------------^
 Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

If the URL provides a proper JSON string your code should work.

Upvotes: 1

Lukasz Kujawa
Lukasz Kujawa

Reputation: 3096

I don't think json_decode stopped working for you. It's rather server response. Did you print $retstr to make sure it's not empty or broken?

Upvotes: 0

Related Questions