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

Advertisement

About Tayeb

Electronic engineer, part-time webmaster.and owner of a tech-oriented, Portugal registered company, "Aliatron".
This entry was posted in Uncategorized. Bookmark the permalink.

10 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 :D

  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

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 )

Connecting to %s