Wednesday, February 17, 2010

PHP Fatal error: Call to undefined function json_encode, How to install JSON in PHP

PHP 5.2 has included inbuilt json_encode() and json_decode() functions by default. Using any version prior to PHP 5.2 doesn't have native JSON support. You need to compile and install json.

JSON stands for "JavaScript Object Notation" is a lightweight data-interchange format. For More Information in JSON Visit JSON Website

Error : "PHP Fatal error: Call to undefined function json_encode()"

How to install JSON in 2 easy steps.
1. Use PECL JSON Package. Log in as root and run the command
root# pecl install json
This will compile the JSON package.
2. Create a file named json.in in /etc/php.d or Check the Module Location in php.ini. Add extension=json.so to the file, and you are done
root#touch /etc/php.d/json.ini
root# echo "extension=json.so" > /etc/php.d/json.in

Restart Apache and run you application.
root# service httpd restart

You have successfully installed JSON.

2 comments:

  1. Hi,

    Thanks for writing this. I've updated PHP to version 5.2.10 and I've followed the steps on this page to install json using pecl. I've ensured /etc/php.d/json.ini contains "extension=json.so" and have restarted httpd.

    Still getting Fatal error: Call to undefined function json_encode()

    I'm using a fresh installation of CentOS 5.3

    Any help would be appreciated, thanks!

    ReplyDelete
  2. Scott, according to ASHWIN "PHP 5.2 has included inbuilt json_encode() and json_decode() functions by default"

    you don't need to install json anymore because you updated your php to 5.2.10. I have 5.1 and installed json and still experiencing the error message "Call to undefined function json_encode()". What could be the problem?

    ReplyDelete