PHP Fatal error: Call to undefined function json_decode()
Solution 1
Using Ubuntu?
Short answer:
sudo apt-get install php7.2-json
(or php7.1-json
or php5-json
depending on the PHP version you're running)
Then of course make sure you restart Apache:
sudo service apache2 restart
Or if you are using PHP-FPM:
sudo service php7.2-fpm restart
(Or php7.1-fpm
or php5-fpm
)
Explanation
Debian has removed the previous JSON extension as of PHP 5.5rc2 due to a license conflict.
The JSON license has a clause which states:
The Software shall be used for Good, not Evil.
This causes a problem with Free Software Foundation's definition of free software which states:
The freedom to run the program, for any purpose (freedom 0).
FSF goes on to specifically list the JSON license as nonfree.
Yes it seems a bit silly. Nevertheless Debian has removed the non-compliant JSON extension, and instead offered a replacement extension that is functionally equivalent.
To be clear: PHP itself has NOT removed JSON, it's still in master. This is a distro / package manager issue.
We have not removed json and we will never release a version of php without json support built in. Any changes in 5.5 is due to whatever distro packaging you are using which we have no control over.
More details
http://iteration99.com/2013/php-json-licensing-and-php-5-5/
http://liorkaplan.wordpress.com/2013/06/01/bye-bye-non-free-php-json-extension/
https://bugs.php.net/bug.php?id=63520
http://philsturgeon.co.uk/blog/2013/08/fud-cracker-php-55-never-lost-json-support
Solution 2
With Ubuntu :
sudo apt-get install php5-json
sudo service php5-fpm restart
Solution 3
Solution for LAMP users:
apt-get install php5-json
service apache2 restart
Solution 4
As a RHEL 8 user, this was fixed with:
yum install php-json
And then reloading nginx and php-fpm. So basically the json libraries where missing from the default php install.
This should also work on CENTOS 8.
Solution 5
If you're using phpbrew try to install json extension to fix error with undefined function json_decode():
phpbrew ext install json
Related videos on Youtube
Comments
-
MirroredFate almost 2 years
Apache is logging
PHP Fatal error: Call to undefined function json_decode()
. After some googling, it seems this problem is a result of not having the latest version of php. Oddly, runningphp --version
ouputsPHP 5.5.1-2+debphp.org~precise+2 (cli) (built: Aug 6 2013 10:49:43) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies with Zend OPcache v7.0.2-dev, Copyright (c) 1999-2013, by Zend Technologies with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
Clearly showing that my
php
version is greater than5.1
.Any ideas as to what could cause this, or the steps to take to find out?
EDIT: The result of a script echoing phpversion is
5.5.1-2+debphp.org~precise
EDIT: The result of
phpinfo()
is alsoPHP Version 5.5.1-2+debphp.org~precise+2
Furthermore, in the
phpinfo()
, it lists the module JSON and the module authors (Omar Kilani, Scott MacVicar).-
Orangepill over 10 yearsCheck the
disable_functions
ini directive to see ifjson_decode
is in that list -
tcmitche over 10 yearsDoes the json module show up in your phpinfo() output?
-
MirroredFate over 10 years
disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_ge\ t_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
-
jgb over 10 yearsUbuntu 12.04 has a package:
php-services-json
. Maybe this helps... -
mario over 10 years@jgb That's the PEAR package Services_Json. Which is also quite encompassing, parses a bit Javascript, not just JSON even. But there are other userland implementations directly reimplementing
json_decode
.
-
-
MirroredFate over 10 yearsI came back to mention that it appears as if
5.5.0
is fine withJSON
, it is only5.5.1
that breaks. -
Luis Lobo Borobia over 10 years5.5.3 does not have it neither (Ubuntu 13.10)
-
Pavel over 10 yearsDon't forget to restart apache after installing json module with the command Chris mentioned.
-
rickfoosusa about 10 yearsIf php-json were a dependency of php-common this would all go away...bugs.launchpad.net/ubuntu/+source/ubiquity/+bug/1280030 ...please vote if this was a problem for you.
-
Jürgen A. Erhard about 10 yearsEver wonder who defines this "Evil" here? And you'd use software that may or may not allow the use you put it to? Me? I wouldn't.
-
recursion.ninja almost 10 yearsDoes anyone find it ironically "evil" that this software licensing exclusion cost me an hour of research time?
-
xilef over 9 yearsIf a restart fails to solve the error, kill the apache2 processes and start: sudo pkill apache2; service apache2 start
-
E Ciotti over 9 yearsstill didn't work, I had to remove and reinstall php5-fpm + extensions (ubuntu 14, PHP 5.5.9-1ubuntu4.5)
-
JRam13 about 9 yearsI just spent a HOURS debugging my php code! Are you kidding me with this... FML
-
gies0r over 7 yearsSame in FreeBSD: Use
pkg install php56-json
-
emix over 5 yearsGuzzle's json_decode is using php's json_decode with additional error check, so your answer is invalid
-
Smit Patel almost 5 yearsWhat about
apt-get install php7.2-json
for 7.2. I tried this but still not working. Giving me error on laravel appphp RuntimeException PHP's json extension is required to use Monolog's NormalizerFormatter