Dec 10 2008

Let’s Install JSON for PHP 5

Originally by Scott Hebert

* his article has been edited to fit in my case.

JSON (JavaScript Object Notation) LogoI recently started messing around with building my own Facebook application. I figured the best way to start learning was to download the demo application and get it working on the Slaptijack web server. I quickly ran into a problem:

PHP Fatal error:  Call to undefined function json_encode()

The problem is that PHP 5.2 includes the json_encode() and json_decode() functions built right in. Unfortunately, I’m running on PHP 5.0 which doesn’t include native JSON support. Here’s how I resolved that.

1. Install json – This was actually trickier than expected. I assumed I would be able to install this via pear. Apparently, a PEAR Services_JSON package was developed, but it has never been accepted into the official repository.

The trick instead is to use the PECL json package. This was as easy as running pecl install json and watch the compiler do its thing.

When it’s done you should have a json.so file in your PHP modules directory. Usually under: /usr/lib/php/modules/

2. Create a file with nano or vi, named : json.ini and put it in to /etc/php.d/ folder – and simply add extension=json.so to this file and that will enable the extension. *skip this step if you don’t have /etc/php.d/ folder*

In other case, you can just need to add: extension=json.so inside your php.ini file. Easy way, you can run below command from your root access :

echo “extension=json.so” >> [directory_path_to]/php.ini

3. Restart Apache - Not much more to add here. Without the restart, the extension won’t be loaded.

4. Profit!

That’s all it took. Now my PHP 5 installation is kicking along happily with the required JSON functions.

Original source:
http://slaptijack.com/system-administration/lets-install-json-for-php-5/