PHP exec() 在命令行中有效,但在 Web 中无效

PHP exec() works in command line but not in web

我正在尝试在 PHP 脚本中使用 jarun 的“googler”来搜索 YouTube 并找到第一个结果的 URL。我正在执行的命令是 googler --np --json -C -n 1 -w youtube.com -x <name of youtube video>,它在我的本地机器上运行良好。这是我的代码:

<?php
exec("googler --np --json -C -n 1 -w youtube.com -x thomas the dank engine", $results);
var_dump($results);
?>

当我在命令行中执行此操作时,它可以正常工作,但当我通过网络浏览器或 GET 请求执行此操作时,它却不起作用。我知道它正在作为另一个用户执行。在我的例子中,它是用户 www-data,所以我给了该用户没有密码的完全 sudo 权限,并执行了以下命令:

sudo -u pi googler --np --json -C -n 1 -w youtube.com -x thomas the dank engine

以及

su - pi -c 'googler --np --json -C -n 1 -w youtube.com -x thomas the dank engine'

这些都不起作用。跟googler有关系吗?我做错了什么?

在命令中添加 2>&1 时,出现以下错误消息:

stdout encoding 'ascii' detected. googler requires utf-8 to work properly. The wrong encoding may be due to a non-UTF-8 locale or an improper PYTHONIOENCODING. (For the record, your locale language is and locale encoding is ; your PYTHONIOENCODING is not set.) Please set a UTF-8 locale (e.g., en_US.UTF-8) or set PYTHONIOENCODING to utf-8.

您必须从 php.ini 文件中的 disable_functions 参数中删除 exec,以便安装 PHP 的服务器模块(与 CLI 安装分开)。服务器模块通常默认禁用它。

尝试放置:

putenv("PYTHONIOENCODING=utf-8");

在调用 exec() 之前的脚本中。 googler 显然需要设置语言环境或此环境变量。