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>
,它在我的本地计算机上运行良好.这是我的代码:
I'm trying to use jarun's "googler" in a PHP script in order to search YouTube and find the URL of the first result. The command I'm executing is googler --np --json -C -n 1 -w youtube.com -x <name of youtube video>
, and it works perfectly on my local machine. Here is my code:
<?php
exec("googler --np --json -C -n 1 -w youtube.com -x thomas the dank engine", $results);
var_dump($results);
?>
当我在命令行中执行此命令时,它可以正常运行,但是当我通过Web浏览器或GET请求执行该操作时,它将不起作用.我知道它正在以另一个用户的身份执行.在我的情况下,它是用户www-data
,因此我为该用户提供了完整的sudo权限(没有密码),并执行了以下命令:
When I execute this in the command line, it works perfectly as it should, but when I do it via a web browser or a GET request, it does not work. I am aware that it is being executed as another user. In my case, it's the user www-data
, so I gave that user full sudo permissions without a password, and did the following commands:
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有关吗?我在做什么错了?
neither of these worked. Does it have to do with googler? What am I doing wrong?
在命令中添加2>&1
时,出现以下错误消息:
When adding 2>&1
to the command, I get the following error message:
检测到stdout编码'ascii'. googler要求utf-8才能正常工作.错误的编码可能是由于非UTF-8语言环境或不正确的PYTHONIOENCODING. (为便于记录,您的语言环境语言为,语言环境编码为;未设置您的PYTHONIOENCODING.)请设置UTF-8语言环境(例如en_US.UTF-8)或将PYTHONIOENCODING设置为utf-8.
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.
推荐答案
尝试放置:
putenv("PYTHONIOENCODING=utf-8");
在调用exec()
之前在脚本中使用
. googler
显然需要设置语言环境或此环境变量.
in the script before calling exec()
. googler
apparently requires the locale or this environment variable to be set.
这篇关于PHP exec()在命令行中工作,但在Web中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!