PHP JSON 函数在被称为 CLI 脚本时不可用?

标签 php json bash ubuntu command-line-interface

刚刚注意到一些东西,但我不确定为什么这是真的,或者为什么它与标准脚本不同,也许有人可以帮我解决这个谜语。运行包含 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/

相关文章:

linux - 如何删除名称为 '' $'\r' 的文件?

php - Python Azure SDK 上传 Blob

arrays - 如何查找从文件中读取的二维数组元素出现的次数?

Python:添加到列表中的字典

json - 如何将具有日期属性的json数据转换为dart

node.js - 如何在适用于 Linux 的 Windows 子系统中下载 nodejs 版本 6.10.3?

bash - 在 bash 的左侧为多部分命令别名

serialization - 序列化 PHP SOAPClient 对象

php - 在 WooCommerce 单一产品标题下显示自定义字段

php - 用 PHP 发布 1 张以上的图片?