Using Drush without json_decode/json_encode enabled.

Did you know that drush requires json support to be enabled in php?

Well, I did not until today.

Fatal error: Call to undefined function json_encode() in /home/drush/includes/output.inc on line 380
Drush command terminated abnormally due to an unrecoverable error.
Error: Call to undefined function json_encode() in /home/drush/includes/output.inc, line 380

If you have root or if your in good terms with your sysadmin, the good solution would be to enable json to your php. Either recompile php or install json using pecl or using your distribution's package manager (yum, rpm, apt, whatever).

Well, I did not have root on that machine so here is what I did.
First I installed pear's Services_JSON package.
Second, I created a file called json.php with the following content found here:

<?php
if ( !function_exists('json_decode') ){
    function json_decode($content, $assoc=false){
                require_once 'Services/JSON.php';
                if ( $assoc ){
                    $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
        } else {
                    $json = new Services_JSON;
                }
        return $json->decode($content);
    }
}

if ( !function_exists('json_encode') ){
    function json_encode($content){
                require_once 'Services/JSON.php';
                $json = new Services_JSON;
               
        return $json->encode($content);
    }
}

Then I created a /.drush/ folder in my home directory and I created a file called php.ini inside that folder.

mkdir ~/.drush
emacs ~/.drush/php.ini

You can use vi if your one of those :)

I put the following inside the file:

auto_prepend_file="/path/to/json.php"

where /path/to/ is the real path of where I put the json.php file above.

That was all and now I could use drush!

Thanks for the informative blog post.

I linked back to these instructions from the Resources page on drush.org (http://www.drush.org/resources).

nice!

That is cool. I am glad if it can help someone :)

--
Christian Roy

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You may post PHP code. You should include <?php ?> tags.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options