12

Could someone advise on how to enable the json_encode function in PHP?

I have a clean install of Centos 5.6 and have just installed Virtualmin.

Does anyone know the next steps to take?

2
  • What version of PHP have you installed?
    – ace
    Sep 6, 2011 at 10:10
  • 3
    What's the problem when you call json_encode() function? What kind of error message do you get?
    – Pavel S.
    Sep 6, 2011 at 10:12

4 Answers 4

14

As in http://www.php.net/manual/en/json.requirements.php

Requirements

There is no installation needed to use these functions; they are part of the PHP core.

Just PHP >= 5.2.0

Edit: saw in a comment also this

On RHEL5/CentOS5 add the EPEL repository (http://fedoraproject.org/wiki/EPEL).

After that, installation is as simple as:

sudo yum install php-pecl-json

Don't know if this apply for your question, I don't have a Centos server...

For Ubuntu:

sudo aptitude install php5-json
3
  • If I run that command it returns saying, error php53-common conflicts with php-common.
    – Rob
    Sep 6, 2011 at 10:19
  • As I said, I don't know if you really need that, it's just an hint. Please update your question with more details, what php version do you have (output of php -v) and what's the output of php -r 'echo json_encode(true);'?
    – Fabio
    Sep 6, 2011 at 10:23
  • Sorry, my mistake - schoolboy error. PHP 5.1!! All sorted now.
    – Rob
    Sep 6, 2011 at 10:28
2

In my case, I came across this question because I was re-compiling PHP will ./configure --disable-all and then individually adding in the extensions I needed. For the case of json_encode(), this is of course included in the JSON extension. To enable this extension, add the option --enable-json to your configure command, i.e.:

./configure --disable-all --enable-json ...

(The "..." refers to the fact that you may have other extensions you may wish to enable.)

In general, any PHP extension can be enabled either by including the option --with-extname or --enable-extname (where "extname" is the name of the extension). You can figure out which one of these two syntaxes to use by issuing ./configure --help | grep extname. Of course, you need to have the library itself available and may have to configure the path where it is located (depending on the extension and whether it is in the "default" path).

1

For php 7 I did this to install json (after hours of struggling):

yum install php70u-json
service nginx restart
service php-fpm restart
1
  • first installed on 2nd gives error Failed to restart nginx.service: Unit not found. Sep 27, 2018 at 7:13
0

On CenotOS and other *nix systems, even if you have PHP compiled with --disable-json not everything may be lost. The first thing of course is to check output from your phpinfo(); and search for occurrences of json. If you see json support enabled then of course you have json extension enabled, but if not, see if you have /etc/php.d/json.ini listed in Additional .ini files parsed. If you do, then this file may look like this:

; Enable json extension module
;extension=json.so

Uncomment the second line and restart your Apache. If Apache started without errors, check your phpinfo() again and see if this enabled your json extension. If if did, you're all set, if not, you may have to find the directory where your php extensions are located and if you see json.so in there, edit the last line to include the full path to that file and restart Apache again. If you don't have json.so file with your other php extension files, you probably will have to recompile that one extension (you don't need to recompile your whole PHP, but you certainly could, this time with --enable-json), and then try again.

Also, if you don't have additional .ini files parsed, you can try adding this extension in your main php.ini, which is usually /etc/php.ini.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.