PHP JSON extension not avaliable

2447 ワード

According to php.net, “there is no installation needed to use [json_encode() and json_decode()]; they are part of the PHP Core“. While it’s certainly true that they are part of the source code, many of the binary packages available for RPM-based platforms (like Red Hat, CentOS and Fedora) have them disabled by default.
There are a number of extensions (PEAR and otherwise) available as RPMs in the official repositories, but sadly not php-json. To enable these functions on CentOS (and by nature, RHEL) 5, the process is simple but not immediately obvious:
  • Make sure you have php, pear and the necessary development packages installed:
     $ sudo yum install php php-pear php-devel $ sudo yum install gcc make 
  • Use PECL to download the json package:
     $ sudo pecl download json 
  • Owing to some odd default memory settings we can’t download and install the package with PECL, we have to use PEAR:
     $ sudo pear install json-1.2.1.tgz 
    PEAR will then trundle through and handle the configure , make and make install commands for you
  • Create a new file in /etc/php.d called json.ini, containing the following:
     ; php-json package - http://pecl.php.net/package/json extension=json.so 
  • Reload apache:
     $ sudo service httpd reload 
  • Test your work:
     $ php -r 'var_dump(function_exists("json_encode"));' 
    All being well, that’ll return bool(true)

  • …and there you have it.
    Note: There’s a chance you’ll run in to an error that looks like this:
     /bin/sh: bad interpreter: Permission denied Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable is set correctly and then rerun this script. 

    … most likely, this is because your temporary partition is mounted with ‘noexec’, check the output of mount and look for the line that refers to/tmp to confirm the presence of ‘noexec’ in the mount options. In this instance you’ll either have to re-mount/tmp without noexec (by removing it from /etc/fstab ) or untar, phpize , configure , make and make install manually.