Introduction
On moving to my new CentOS server (with php version 5.1.6), the php code for automatically twitting latest posts from my forums to Twitter stopped working (check the php code here A Twitbot Using OAUTH).
I will explain below how the errors were determined and fixed. This post may help others to fix a similar problem.
Determine the symptoms
- Enable error messages to be shown by writing ini_set( ‘ display_errors ‘, ‘ 1′); in your PHP code.
- If the error message is “Fatal Error Call to Undefined Function Json_decode ()”, read on…
The solution by installing JSON PHP Extension
1 . Ssh into your webserver with root privileges
2 . Install gcc make:
# yum install gcc make
3 . Install php-pear and php-devel :
# yum install php-pear php-devel
4 . Install json:
# pecl install json
If there are no dependency errors, or other, and all packages duly install you will next:
<
- Open your php.ini file (ex. nano /etc/php.ini)
- Find in php.ini a section called Dynamic Extensions
- Add a line: extension = json.so
- Save php.ini and restart Apache (ex. service httpd restart)
You can check if json is enabled, installing in your httpdocs folder a php file (ex. test.php) with the following code:
<?php
phpinfo();
?>
If you open this page in your browser, you will see information on what your server’s php supports, and surely json will be one fo them:
Finally test json, with following php code (ex. test_json.php) in your httpdocs folder as shown in php.net website
<?php
$json = ‘{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}’;
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
If you open the file in your browser, the result will be:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
Json_decode is now working in your server, and there will be no more error reports such as “Fatal Error Call to Undefined Function Json_decode()”.

Excellent – worked great – thanks so much!!
I’m glad it worked for you!
Thank very much, yes i need it and working
You are welcome. Glad you fixed the error.
Thank
, my server is CentOS server with php version 5.1.6. I need Json for facebook connect. I found here and my problem solved
.
Excellent, and I’m glad it worked for you.
thanks for this post, solves my problems
You’re welcome, and glad it worked for you.
Hi,
I am on RedHat (Oracle Enterprise Linux to be more exact). And I have issue installing or enabling the json. Would appreciate if you could through an idea. I did
cd /home/oracle/php-5.3.10/ext/json
[root@e6400l json]# phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
Then added the line
extension = /usr/local/lib/php/extensions/no-debug-non-zts-20090626/json.so
to php.ini, in my case the php.ini is under /usr/local/lib, but I am still getting the error.
And the
doesn’t show I have json support. How do I enable the json in my PHP, this is 5.1.6
Thanks much
Anatoliy
Welcome to my blog. I don’t know sincerely how to help you. I shared here CentOS solution as I was also learning how to do it and since it worked perfectly others could benefit