0

Okay, This might sound like a rookie mistake but I swear it isn't, I use PHP every day and never had this issue, But when trying to convert a perfectly valid JSON document

[
    {
        "title" : "Introduction",
        "location" : "/class/html/",
        "des" : "An introduction to HTML!"
    }
]

with the following PHP code

$json = file_get_contents("assets/pages/html/pages.json");
$decoded = json_decode($json,true);
var_dump($decoded);

Nothing happens, I get a 500 error when I try to run it, Then when I go into windows edge debugger it just does blank, Did I do something wrong? I have searched the internet all day and nothing has worked

PHP V7.1

EDIT:

I checked the file location multiple times and it works perfectly, Even if a directly input the string it still doesn't work, When I check the error_log I get the following error PHP Fatal error: Uncaught Error: Call to undefined function json_decode()

THIS IS MY PHP.INI

display_errors = On
max_execution_time = 30
max_input_time = 60
max_input_vars = 1000
memory_limit = 128M
post_max_size = 8M
session.gc_maxlifetime = 1440
session.save_path = "/var/cpanel/php/sessions/ea-php70"
upload_max_filesize = 32M
zlib.output_compression = Off
Phayden
  • 21
  • 1
  • 6
  • 1
    This is likely coming from `file_get_contents`, the 5XX status code is a server error. My first guess is that the file doesn't exist at that location, to confirm take a look at this fiddle using a literal instead: https://3v4l.org/JJaED – David Aug 27 '18 at 20:49
  • Does it work when you hard code the JSON into the $json variable, and then use the `json_decode()` function? That would be a clue that your file is not at that path – Barney Chambers Aug 27 '18 at 20:54
  • be sure abou the relative path.... try with the entire path.. and about the 500 error try the error_all – Leo Nogueira Aug 27 '18 at 20:59
  • 1
    If you're getting a 500 error, check the PHP error log on the server. – Barmar Aug 27 '18 at 21:49
  • See https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Barmar Aug 27 '18 at 21:49
  • I can directly input the JSON string [{"title":"Introduction","location":"/class/html/","des":"An introduction to HTML!"}] and the same error is returned – Phayden Aug 28 '18 at 02:31
  • When I checked the error_log I get this error `PHP Fatal error: Uncaught Error: Call to undefined function json_decode()` – Phayden Aug 28 '18 at 02:35

1 Answers1

0

I managed to fix it, after hours of messing around, I fixed it by using JSON_Services,

function json_decode($str) {
    $json = new Services_JSON();
    $str = $json->decode($str);
    $str = (array) $str;
    return $str;
}
Phayden
  • 21
  • 1
  • 6
  • That looks like some third-party pure PHP library. [json_decode()](http://php.net/json_decode) is a native extension since PHP/5.2.0, you can also enable it or ask your hosting provider to do it for you (it's a pretty basic feature). – Álvaro González Aug 28 '18 at 17:45
  • I did that, they couldn’t figure out what was wrong so I made that function to make up for the lack of basic functions apparently – Phayden Aug 28 '18 at 17:48