简体   繁体   中英

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?

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM