PHP 致命错误:调用未定义的函数 json_decode()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18239405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:13:36  来源:igfitidea点击:

PHP Fatal error: Call to undefined function json_decode()

phpjsonapacheubuntu

提问by MirroredFate

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, running php --versionouputs

Apache 正在记录PHP Fatal error: Call to undefined function json_decode()。经过一番谷歌搜索,似乎这个问题是由于没有最新版本的 php。奇怪的是,运行php --version输出

PHP 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 phpversion is greater than 5.1.

清楚地表明我的php版本大于5.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

编辑:脚本回显 phpversion 的结果是 5.5.1-2+debphp.org~precise

EDIT: The result of phpinfo()is also PHP Version 5.5.1-2+debphp.org~precise+2

编辑:结果phpinfo()也是PHP 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).

此外,在 中phpinfo(),它列出了模块 JSON 和模块作者(Omar Kilani、Scott MacVicar)。

回答by jszobody

Using Ubuntu?

使用 Ubuntu?

Short answer:

简短的回答:

sudo apt-get install php7.2-json

(or php7.1-jsonor php5-jsondepending on the PHP version you're running)

(或php7.1-jsonphp5-json取决于您正在运行的 PHP 版本)

Then of course make sure you restart Apache:

然后当然要确保重新启动 Apache

sudo service apache2 restart

Or if you are using PHP-FPM:

或者,如果您使用的是 PHP-FPM:

sudo service php7.2-fpm restart

(Or php7.1-fpmor php5-fpm)

(或php7.1-fpmphp5-fpm

Explanation

解释

Debian has removed the previous JSON extension as of PHP 5.5rc2 due to a license conflict.

由于许可证冲突,Debian 从 PHP 5.5rc2 起删除了之前的 JSON 扩展。

The JSON licensehas a clause which states:

JSON许可证具有规定的条款:

The Software shall be used for Good, not Evil.

该软件应用于善,而不是用于恶。

This causes a problem with Free Software Foundation's definition of free softwarewhich states:

这会导致自由软件基金会对自由软件的定义出现问题,该定义指出:

The freedom to run the program, for any purpose (freedom 0).

出于任何目的运行程序的自由(自由 0)。

FSF goes on to specifically list the JSON license as nonfree.

FSF 继续特别将 JSON 许可证列为 nonfree

Yes it seems a bit silly. Nevertheless Debian has removed the non-compliant JSON extension, and instead offered a replacement extensionthat is functionally equivalent.

是的,这似乎有点傻。尽管如此,Debian 已经删除了不兼容的 JSON 扩展,取而代之的是提供了一个功能等效的替代扩展

To be clear: PHP itself has NOT removed JSON, it's still in master. This is a distro / package manager issue.

需要明确的是:PHP 本身并没有删除 JSON,它仍然在 master 中。这是发行版/包管理器问题。

Rasmus makes it pretty clear:

拉斯穆斯说得很清楚

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.

我们没有删除 json 并且我们永远不会发布没有内置 json 支持的 php 版本。 5.5 中的任何更改都是由于您使用的发行版包装,我们无法控制。

More details

更多细节

http://iteration99.com/2013/php-json-licensing-and-php-5-5/

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/

http://liorkaplan.wordpress.com/2013/06/01/bye-bye-non-free-php-json-extension/

https://bugs.php.net/bug.php?id=63520

https://bugs.php.net/bug.php?id=63520

http://philsturgeon.co.uk/blog/2013/08/fud-cracker-php-55-never-lost-json-support

http://philsturgeon.co.uk/blog/2013/08/fud-cracker-php-55-never-lost-json-support

回答by David

With Ubuntu :

使用 Ubuntu:

sudo apt-get install php5-json
sudo service php5-fpm restart

回答by milkovsky

Solution for LAMP users:

LAMP 用户的解决方案:

apt-get install php5-json
service apache2 restart

Source

来源

回答by yesnik

If you're using phpbrewtry to install jsonextension to fix error with undefined function json_decode():

如果您使用的是phpbrew,请尝试安装json扩展以修复未定义函数 json_decode() 的错误:

phpbrew ext install json

回答by Carl Hinton

The same issue with 7.1

与 7.1 相同的问题

apt-get install php7.1-json sudo nano /etc/php/7.1/mods-available/json.ini

apt-get install php7.1-json sudo nano /etc/php/7.1/mods-available/json.ini

  • Add json.so to the new file
  • Add the appropriate sym link under conf.d
  • Restart apache2 service (if needed)
  • 将 json.so 添加到新文件中
  • 在 conf.d 下添加适当的符号链接
  • 重启 apache2 服务(如果需要)

回答by Rafy

I have the same question: PHP Fatal error: Call to undefined function json_decode(), but I run php under cygwin on Windows. When I run php -m, I found that there is no json module installed. So I run cygwin setup.exe again, check json package from the configuration interface, and the problem is solved.

我有同样的问题:PHP Fatal error: Call to undefined function json_decode(),但我在 Windows 上的 cygwin 下运行 php。运行的时候php -m发现没有安装json模块。于是再次运行cygwin setup.exe,从配置界面查看json包,问题解决。

回答by NicoMinsk

The module was install but symbolic link was not in /etc/php5/cli/conf.d

该模块已安装,但符号链接不在 /etc/php5/cli/conf.d 中

回答by jaxxed

you might also consider avoiding the core PHP module altogether.

您也可以考虑完全避免使用核心 PHP 模块。

It is quite common to use the guzzle json tools as a library in PHP apps these days. If your app is a composer app, it is trivial to include them as a part of a composer build. The guzzle tool, as a library, would be a turnkey replacement for the json tool, if you tell PHP to autoinclude the tool.

如今,在 PHP 应用程序中使用 guzzle json 工具作为库是很常见的。如果您的应用程序是 Composer 应用程序,那么将它们作为 Composer 构建的一部分包含在内是微不足道的。如果您告诉 PHP 自动包含该工具,作为库的 guzzle 工具将成为 json 工具的交钥匙替代品。

http://docs.guzzlephp.org/en/stable/search.html?q=json_encode#

http://docs.guzzlephp.org/en/stable/search.html?q=json_encode#

http://apigen.juzna.cz/doc/guzzle/guzzle/function-GuzzleHttp.json_decode.html

http://apigen.juzna.cz/doc/guzzle/guzzle/function-GuzzleHttp.json_decode.html

回答by Joan Teran

CENTOS

Scene

中心

场景

I installed PHP in Centos Docker, this is my DockerFile:

我在 Centos Docker 中安装了 PHP,这是我的 DockerFile:

FROM centos:7.6.1810

LABEL maintainer="[email protected]"

RUN yum install httpd-2.4.6-88.el7.centos -y
RUN rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
RUN rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
RUN yum install php72w -y
ENTRYPOINT ["/usr/sbin/httpd", "-D", "FOREGROUND"]

The app returned the same error with json_decode and json_encode

该应用程序返回了与 json_decode 和 json_encode 相同的错误

Resolution

解析度

Install PHP Common that has json_encode and json_decode

安装具有 json_encode 和 json_decode 的 PHP Common

yum install -y php72w-common-7.2.14-1.w7.x86_64

How to find the resolution?

如何找到分辨率?

I have another Docker File what build the container for the API and it has the order to install php-mysql client:

我有另一个 Docker 文件,它为 API 构建容器,它具有安装 php-mysql 客户端的顺序:

yum install php72w-mysql.x86_64 -y

If i use these image to mount the app, the json_encode and json_decode works!! Ok..... What dependencies does this have?

如果我使用这些图像来安装应用程序,json_encode 和 json_decode 就可以工作了!!好的..... 这有什么依赖关系?

[root@c023b46b720c etc]# yum install php72w-mysql.x86_64
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirror.gtdinternet.com
 * epel: mirror.globo.com
 * extras: linorg.usp.br
 * updates: mirror.gtdinternet.com
 * webtatic: us-east.repo.webtatic.com
Resolving Dependencies
--> Running transaction check
---> Package php72w-mysql.x86_64 0:7.2.14-1.w7 will be installed
--> Processing Dependency: php72w-pdo(x86-64) for package: php72w-mysql-7.2.14-1.w7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: php72w-mysql-7.2.14-1.w7.x86_64
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: php72w-mysql-7.2.14-1.w7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.60-1.el7_5 will be installed
---> Package php72w-pdo.x86_64 0:7.2.14-1.w7 will be installed
--> Processing Dependency: php72w-common(x86-64) = 7.2.14-1.w7 for package: php72w-pdo-7.2.14-1.w7.x86_64
--> Running transaction check
---> Package php72w-common.x86_64 0:7.2.14-1.w7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================
 Package                   Arch               Version                        Repository            Size
========================================================================================================
Installing:
 php72w-mysql              x86_64             7.2.14-1.w7                    webtatic              82 k
Installing for dependencies:
 mariadb-libs              x86_64             1:5.5.60-1.el7_5               base                 758 k
 php72w-common             x86_64             7.2.14-1.w7                    webtatic             1.3 M
 php72w-pdo                x86_64             7.2.14-1.w7                    webtatic              89 k

Transaction Summary
========================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 2.2 M
Installed size: 17 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): mariadb-libs-5.5.60-1.el7_5.x86_64.rpm                                    | 758 kB  00:00:00     
(2/4): php72w-mysql-7.2.14-1.w7.x86_64.rpm                                       |  82 kB  00:00:01     
(3/4): php72w-pdo-7.2.14-1.w7.x86_64.rpm                                         |  89 kB  00:00:01     
(4/4): php72w-common-7.2.14-1.w7.x86_64.rpm                                      | 1.3 MB  00:00:06     
--------------------------------------------------------------------------------------------------------
Total                                                                   336 kB/s | 2.2 MB  00:00:06     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                 1/4 
  Installing : php72w-common-7.2.14-1.w7.x86_64                                                     2/4 
  Installing : php72w-pdo-7.2.14-1.w7.x86_64                                                        3/4 
  Installing : php72w-mysql-7.2.14-1.w7.x86_64                                                      4/4 
  Verifying  : php72w-common-7.2.14-1.w7.x86_64                                                     1/4 
  Verifying  : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                 2/4 
  Verifying  : php72w-pdo-7.2.14-1.w7.x86_64                                                        3/4 
  Verifying  : php72w-mysql-7.2.14-1.w7.x86_64                                                      4/4 

Installed:
  php72w-mysql.x86_64 0:7.2.14-1.w7                                                                     

Dependency Installed:
  mariadb-libs.x86_64 1:5.5.60-1.el7_5                php72w-common.x86_64 0:7.2.14-1.w7               
  php72w-pdo.x86_64 0:7.2.14-1.w7                    

Complete!

Yes! Inside the dependences is the common packages. I Installed it into my other container and it works! After, i put de directive into DockerFile, Git commit!! Git Tag!!!! Git Push!!!! Ready!

是的!依赖项内部是公共包。我将它安装到我的另一个容器中,它可以工作!之后,我将 de 指令放入 DockerFile,Git 提交!!Git标签!!!git推送!!!准备好!