Fixing: Fatal Error Call To Undefined Function Json_decode

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

  1. Enable error messages to be shown by writing ini_set( ‘ display_errors ‘,  ‘ 1’);  in your PHP code.
  2. 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:
<

  1. Open your php.ini file  (ex. nano /etc/php.ini)
  2. Find in php.ini a section called Dynamic Extensions
  3. Add a line: extension = json.so
  4. 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:

phpinfo() showing json enabled

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()”.

Advertisements

About Tayeb

Electronics engineer, part-time webmaster and owner of "Aliatron", a tech-oriented company registered in Portugal and Mozambique.
This entry was posted in Server Errors and tagged , , , , , , . Bookmark the permalink.

42 Responses to Fixing: Fatal Error Call To Undefined Function Json_decode

  1. pete says:

    Excellent – worked great – thanks so much!!

  2. idham says:

    Thank very much, yes i need it and working 😀

  3. Thomas says:

    thanks for this post, solves my problems

  4. Anatoliy says:

    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

    • Tayeb says:

      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

  5. Keith says:

    Great! you saved me a lot of time.

  6. Connor Tobin says:

    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.

  7. David says:

    Dude, you’re awesome!

  8. Marcos says:

    Thanks! Works great! Nice explain.

  9. live chat says:

    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.

  10. 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.

  11. luxy says:

    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

  12. Rio says:

    salam,
    what if I just a website administrator, not the one who handle the server? can I fix it through my admin page?
    thanks

  13. ron says:

    Excellent solution! Got php working with json real quick. Thanks

  14. ppiper says:

    This helped a LOT! Thx m8!
    Btw. I needed to install the php-json mod as well .. so this might be another blocker.

  15. mikeb says:

    Thanks alot!
    That fixed an error I got when trying to send call notifications into an asterisk pbx to twitter

  16. Pingback: Ngihik Centos: JSON di PHP 5.1.x | [go2n@buglink:~] $

  17. Sok Sovat says:

    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!

    • Tayeb says:

      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.

  18. Tony says:

    Thank you! Works perfectly for Google reCaptcha which needs JSON

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s