본문 바로가기

IT-Consultant

CentOs에서 json_decode 함수 사용하기

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:

  1. 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
  2. Use PECL to download the json package:
    $ sudo pecl download json
  3. 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 configuremake and make install commands for you

  4. 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
  5. Reload apache:
    $ sudo service httpd reload
  6. 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,configuremake and make install manually.