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 :D, 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.
can you send me your skill profile to me directtouch@gmail.com
Welcome to my blog. Thank you for your offer. But I don’t need any work. I have my own business and I am quite ok. You may like to know that most of hits of this blog come from USA and India. That means indians are very much interested in tech matters even insignificant ones that I share here.
Thank you Jignesh for responding.
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
Great! you saved me a lot of time.
Welcome to my blog. I am glad it worked for you and saved you time.
THANK YOU. Sincerely. I was on a tight deadline to get facebook graph data integrated into a site, and I ran into this problem.
Great fix, thank you.
Welcome to the blog. I am glad you were be able to fix the error.
Dude, you’re awesome!
I am glad it worked for you.
Thanks! Works great! Nice explain.
Welcome to the blog. I am glad it worked for you.
Cheers, Worked fine here on:
Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Bit strange why the yum install itself didnt create the .so file. odd why you needed to compile.
But yeah, worked fine.
Welocme to my blog. Glad it worked for you.
Brilliant.
I moved up to Centos 6 and was getting a white screen upon joomla admin login. I googled
PHP Fatal error: Call to undefined function json_decode() in /home/
I found your page, followed your instructions and it fixed the issue.
Thanks so much for posting this.
Welcome to my blog. Sharing is the idea and knowledge belongs to all. I am glad the article helped you.
This work great but when i reinstalled JCE, it came back to the same problem, no i can even have an editor working on the site, i have to select no editor in global config else i get this ”
Fatal error: Call to undefined function json_decode() in /var/www/html/…/administrator/components/com_jce/models/model.php on line 31″ or a blank page
Welcome to my blog. I have no idea what happened. Well the error says undefined json_decode!
salam,
what if I just a website administrator, not the one who handle the server? can I fix it through my admin page?
thanks
Welcome to my blog. You need root access to your server and permissions to install software.
tks, you save my life!
Welcome to my blog. I am glad it worked for you!
Excellent solution! Got php working with json real quick. Thanks
Welcome to my blog. I am glad it worked for you.
This helped a LOT! Thx m8!
Btw. I needed to install the php-json mod as well .. so this might be another blocker.
Welcome to the blog. I am glad you got it working.
Thanks alot!
That fixed an error I got when trying to send call notifications into an asterisk pbx to twitter
Welcome to the blog. I am glad you were able to benefit from my post.
Pingback: Ngihik Centos: JSON di PHP 5.1.x | [go2n@buglink:~] $
That is great! But i have a problem with installing JSON PHP Extension. I’m using CentOS 5.3, 64bits. When i try to install using command yum, it is unknown command yam. Do you know how to install command yum?
Thank in advance!
Welcome to my blog. My server has yum pre-installed by the Net Center. So effectively to use yum you need to access RPM repository. I suggest you do a bit of reading before going ahead. The following Wiki has the reputation of giving good guidelines:
http://wiki.centos.org/AdditionalResources/Repositories/RPMForge
If you succeed, or not, please post here what happened. It may be useful to others.
Note you must have some basic skills in doing what you want to do, otherwise I advise you seek help from your server’s providers, or from more knowledgeable friends or colleagues.
Thank you! Works perfectly for Google reCaptcha which needs JSON
Thank you Tony. I’m glad it worked