I 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.
- 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 runningpecl install json
and watching the compiler do its thing. When it's done you should have ajson.so
file in your PHP modules directory. (Mine is/usr/lib/php/modules/
.) - Add
json.ini
file to/etc/php.d/
- This file is pretty simple. Simply addextension=json.so
to this file and that will enable the extension. - Restart Apache - Not much more to add here. Without the restart, the extension won't be loaded.
- Profit!
That's all it took. Now my PHP 5 installation is kicking along happily with the required JSON functions.
Related Reading:
- Introduction to JavaScript Object Notation: a to-the-point guide to JSON
- PHP Web Services: APIs for the Modern Web
[...] Source: http://slaptijack.com/system-administration/lets-install-json-for-php-5/ [...]
Great tutorial.
This information is still valid in 2010 and works wonders for getting certain plugins to work flawlessly with WordPress!! Thanks for the great info!!
until I discovered this tutorial my only option was to upgrade the packages on my RH EL5 server. This would have rendered my server as 'non-official' with RedHat Network (a rather large price to pay).
Your solution took 5 mins to implement and worked like a charm.
Thanks Heaps :)
If you are working on a shared web server which have older php version. You can include jsonwrapper.php file to overcome this. include"jsonwrapper.php" At the top of your code and you're ready to go.
Here is the location to download jsonwrapper.php file
http://www.boutell.com/scripts/jsonwrapper.html
Hope this will help..:D
thanks dilantha, this is what i were looking for, i will try it and post here the results.
json test file gives the following result:
Warning: main(PHPUnit.php): failed to open stream: No such file or directory in /usr/local/pem/vhosts/238423/webspace/httpdocs/wordpress/fb/phpsdk/src/JSON/Test-JSON.php on line 22 Fatal error: main(): Failed opening required ‘PHPUnit.php’ (include_path=’.:/php/includes’) in /usr/local/pem/vhosts/238423/webspace/httpdocs/wordpress/fb/phpsdk/src/JSON/Test-JSON.php on line 22
help please
thanks a lot man...it worked for me
Hi,
Thanks a lot for your trick, I am installing glpi on a customer with redhat el 5, the version of php is version 5.1.6, your trick saved my life :D
Thanks a lot. It works
Glad to hear it!
[...] install JSON in your linux machine, follow these [...]
Thank you!
json test file gives the following result:
Warning: main(PHPUnit.php): failed to open stream: No such file or directory in /usr/local/pem/vhosts/238423/webspace/httpdocs/wordpress/fb/phpsdk/src/JSON/Test-JSON.php on line 22 Fatal error: main(): Failed opening required 'PHPUnit.php' (include_path='.:/php/includes') in /usr/local/pem/vhosts/238423/webspace/httpdocs/wordpress/fb/phpsdk/src/JSON/Test-JSON.php on line 22
help please :(
It looks to me like PHPUnit.php is missing.
[...] install JSON in your linux machine, follow these [...]
[...] a quick bit of googling I came across an excellent tutorial for installing the said module, however I then ran into another problem after trying to execute [...]
I'm using CentOS 4.4 and PHP 5.1.6
I've followed the steps above.
Now the command line PHP (php -m) shows the json module is loaded.
However the Apache version of php doesn't loads the .SO file (however it reads the json.ini file). Of course I've restarted the webserver.
Any advice?
Finally I've resolved the problem (Apache+PHP 5.1.6+JSON working together). Solution: http://fanaticalwebworker.blogspot.com/2010/10/json-with-apache-centos-44-and-php-516.html
Great! Thanks for posting the solution!
Great instructions I'm glad I didn't need to install from source! Just one note for those less Savy. I was installing on RHEL 4 for a client and received the warning:
pecl install json
WARNING: channel "pear.php.net" has updated its protocols, use "channel-update pear.php.net" to update
downloading json-1.2.1.tgz ...
Starting to download json-1.2.1.tgz (17,780 bytes)
......done: 17,780 bytes
11 source files, building
running: phpize
sh: phpize: command not found
ERROR: `phpize' failed
If this happens to you the solution is to install the php-development RPM.
Thanks, Doug!
Install JSON PHP
1. yum install gcc make
2. yum install php php-pear php-devel
3. pecl download json
4. pear install json-1.2.1.tgz
if it does not work use
4. yum install php-json
5. service httpd restart
and see youself
it worked for me
thanks
Thanks a bunch. Saved me a lot of research.
thanks a lot. saved me a lot of research during a GLPI install.
Thanks a lot, have been trying several guideline, but this solution is the only one that can help me to install GPLI with PHP 5.1.6..
Thank you thank you thank you..
I follow the tutorial but ran into a tricky problem. The json extension was not recognize by "Apache" server
http://fanaticalwebworker.blogspot.com/2010/10/json-with-apache-centos-44-and-php-516.html
On ubuntu i got this to work by doing:
apt-get install php5-json
this installed and added the module to the php 5.1.2
This worked for me too on php 5.1.6 It took 5 minutes to get going with this solution.
Thanks for such a simple solution.
grt thanks, it worked for me, thanks for the solution
cheers
venki
[...] http://slaptijack.com/system-administration/lets-install-json-for-php-5/ This entry was posted in Facebook, JSON, PHP, Server Admin and tagged bash pecl command not found, is not in the sudoers file, ssh install pear, usr/bin/pear no such file or directory. Bookmark the permalink. ← FB.ui( message position [...]
Great tutorial. In case you don't have pecl on your box, which was the case with my server, install php-pear (yum install php-pear) and it will download and install pecl
open php.ini file and add to extension to
extension=php_json.dll
brilliant thanks
or
extension=json.so
on a Linux system.
This can either be in the main php.ini (/etc/php.ini) or as a separate ini file in the /etc/php.d directory.
I hope this helps anyone who is getting the "Fatal error: Call to undefined function: json_encode() " error.
Thanks for the instructions; worked like a charm!
Cheers,
Mark
Thanks it works for me also ,
have configured on php 5.1.6 version on Gcloud Hosting server.
Regards,
vijay kadadi
Thanks, this worked perfect!
Agradecido :)
got it, thanks~
Thank you , you saved my day ;)
Glad to hear it! Thanks for leaving a note.