0

I know there have been other topics regarding this error, but I'm asking how do I confirm if this is available on my hosted server and if it isn't how do I add it:

phpinfo returns:

PHP Version 5.5.14
JSON    Omar Kilani, Scott MacVicar

Yet my error log shows: PHP Fatal error: Call to undefined function json_encode() in

I thought 5.5.14 should have included JSON ?

Thanks

5
  • 2
    use php -m and see if json module is being loaded Commented Jan 31, 2017 at 11:18
  • Thanks - that shows it's not being loaded.. How do I load it ? Commented Jan 31, 2017 at 11:22
  • Which OS you are using ? Commented Jan 31, 2017 at 11:24
  • First you need to install this module and add into php.in like extension= json.so, depending on os Commented Jan 31, 2017 at 11:24
  • I'm running openSuSe 13.2 Commented Jan 31, 2017 at 11:26

2 Answers 2

2

Just install the JSON package with PECL because not every PHP distribution has the JSON package due to licensing restrictions.

Installation instructions here

Sign up to request clarification or add additional context in comments.

Comments

0

Here's what I've used successfully for PHP 5.1

  function json_encode($a=false)
    {
        if (is_null($a)) return 'null';
        if ($a === false) return 'false';
        if ($a === true) return 'true';
        if (is_scalar($a))
        {
            if (is_float($a))
            {
                // Always use "." for floats.
                return floatval(str_replace(",", ".", strval($a)));
            }

            if (is_string($a))
            {
                static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
                return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
            }
            else
            return $a;
        }
        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a))
        {
            if (key($a) !== $i)
            {
                $isList = false;
                break;
            }
        }
        $result = array();
        if ($isList)
        {
            foreach ($a as $v) $result[] = json_encode($v);
            return '[' . join(',', $result) . ']';
        }
        else
        {
            foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
            return '{' . join(',', $result) . '}';
        }
    }

  function json_decode($json) 
  {  

    $comment = false; 
    $out = '$x='; 

    for ($i=0; $i<strlen($json); $i++) 
    { 
        if (!$comment) 
        { 
            if ($json[$i] == '{' || $json[$i] == '[')        $out .= ' array('; 
            else if ($json[$i] == '}' || $json[$i] == ']')    $out .= ')'; 
            else if ($json[$i] == ':')    $out .= '=>'; 
            else                         $out .= $json[$i];            
        } 
        else $out .= $json[$i]; 
        if ($json[$i] == '"')    $comment = !$comment; 
    } 
    eval($out . ';'); 
    return $x; 
} 

1 Comment

I would suggest using built-in functions instead. There is no guarantee that custom solutions can handle all edge cases and are safe from exploits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.