Problems with JSON and PHP

Doug Hardie bc979 at
Fri Feb 1 06:41:34 UTC 2019

I have installed apache24, php72 and a host of other somewhat related php packages.  I can't seem to get apache to run a php script that includes json_encode.

test# pkg info | grep php
mod_php72-7.2.14               PHP Scripting Language
php72-7.2.14                   PHP Scripting Language
php72-curl-7.2.14              The curl shared extension for php
php72-filter-7.2.14            The filter shared extension for php
php72-gd-7.2.14                The gd shared extension for php
php72-hash-7.2.14              The hash shared extension for php
php72-json-7.2.14              The json shared extension for php
php72-mbstring-7.2.14          The mbstring shared extension for php
php72-mysqli-7.2.14            The mysqli shared extension for php
php72-openssl-7.2.14           The openssl shared extension for php
php72-pear-1.10.6              PEAR framework for PHP
php72-pear-Services_JSON-1.0.3 PHP implementation of json_encode/decode
php72-pecl-mcrypt-1.0.1        PHP extension for mcrypt, removed in PHP 7.2
php72-pecl-xdebug-2.6.1        Xdebug extension for PHP
php72-session-7.2.14           The session shared extension for php
php72-xml-7.2.14               The xml shared extension for php
php72-zip-7.2.14               The zip shared extension for php
php72-zlib-7.2.14              The zlib shared extension for php

test# pkg info | grep apache
apache24-2.4.38                Version 2.4.x of Apache web server

test# pkg info | grep mod_
mod_php72-7.2.14               PHP Scripting Language

Server has been rebooted several times.

test# freebsd-version -ku

In /usr/local/www/apache24/data is the file:
test# more /usr/local/www/apache24/data/test.php
print "Hello world";

$myObj = (object) array();
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";

$myJSON = json_encode($myObj);

echo $myJSON;

Running it by hand works:
test# php /usr/local/www/apache24/data/test.php
Hello world{"name":"John","age":30,"city":"New York"}test# 

Accessing it via a browser yields:
Hello world

and an error:
test# tail -1 /var/log/httpd-error.log
[Thu Jan 31 22:29:25.506018 2019] [php7:error] [pid 1586] [client] PHP Fatal error:  Uncaught Error: Call to undefined function json_encode() in /usr/local/www/apache24/data/test.php:9\nStack trace:\n#0 {main}\n  thrown in /usr/local/www/apache24/data/test.php on line 9

The info.php file in that directory works properly when accessed via the browser.  Obviously I have got something setup wrong.  I have not been able to figure it out.  ktrace never shows any attempt to access anything I can see identifiable with json_encode.  I would appreciate any ideas on how to fix this.  Thanks

-- Doug

More information about the freebsd-questions mailing list