0

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);
?>

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

as well as

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

neither of these worked. Does it have to do with googler? What am I doing wrong?

When adding 2>&1 to the command, I get the following error message:

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.

7
  • Maybe googler isn't in its $PATH, try using the full pathname. Commented Jul 5, 2017 at 23:30
  • 1
    Capture the error output with 2>&1 at the end of the command being sent to exec(). Commented Jul 5, 2017 at 23:30
  • What's the actual error that PHP gives you? Commented Jul 5, 2017 at 23:30
  • "so I gave that user full sudo permissions without a password" - this is never the way to go about things! Commented Jul 6, 2017 at 0:05
  • 1
    Environment variables are set in ~/.profile and /etc/profile when you login. The webserver doesn't login interactively, so it doesn't execute a profile script. The googler script is apparently dependent on the locale setting, and those variables aren't set. Try putting putenv("LC_ALL=en_US.UTF-8"); in the PHP script before calling exec(). Commented Jul 6, 2017 at 1:00

2 Answers 2

2

Try putting:

putenv("PYTHONIOENCODING=utf-8");

in the script before calling exec(). googler apparently requires the locale or this environment variable to be set.

Sign up to request clarification or add additional context in comments.

Comments

0

You must remove exec from the disable_functions parameter in the php.ini file for your server module installation of PHP (which is separate from your CLI installation). It is typically disabled by default for the server module.

2 Comments

I've been able to do exec("whoami") successfully, so this is not the issue.
I apologize for the incorrect answer. I gave my best guess based on the information provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.