刚刚注意到一些东西,但我不确定为什么这是真的,或者为什么它与标准脚本不同,也许有人可以帮我解决这个谜语。运行包含 json_*
的 PHP 脚本时命令行中的函数作为可执行文件我将得到 Fatal error: Call to undefined function json_encode() in /var/www/jsontest.php on line 6
使用下面的脚本
#!/usr/bin/php -n
<?php
$arr = array('foo', 'bar', 'baz');
print_r($json = json_encode($arr));
print_r(json_decode($bar));
尝试解码标准干净 json 输入(通过 jsonlint 验证)时,json_decode() 也会发生这种情况
上面的脚本在 Linux/DEB 终端中运行如下...
$ chmod +x jsontest.php
$ ./jsontest.php
不过,通过我的本地网络服务器运行它,我收到了预期的输出
#!/usr/bin/php -n ["foo","bar","baz"]Array ( [0] => foo [1] => bar [2] => baz )
到底是怎么回事?为什么 JSON 在解释为可执行文件时不可用?
PHP版本是
PHP 5.5.3-1ubuntu2 (cli) (built: Oct 9 2013 14:49:24)
和 PHPInfo 对于任何可能想要它的人都发布在我的 Ubuntu One Account 上。
最佳答案
我猜这里的问题是你的 php.ini
文件 - PHP 使用不同的 ini
作为 CLI 运行时的文件。看看Explosion Pill's answer on this question :
When running php from the command line, you can use the -c or --php-ini argument to point to the php.ini file to use. This will allow you to use one php.ini file for both. You can also alias php to php -c/path/to/php.ini if you are running the script yourself.
关于PHP JSON 函数在被称为 CLI 脚本时不可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20407255/