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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Jamsheed Mistri
  • 419
  • 1
  • 4
  • 19
  • Maybe `googler` isn't in its `$PATH`, try using the full pathname. – Barmar Jul 05 '17 at 23:30
  • 1
    Capture the error output with `2>&1` at the end of the command being sent to `exec()`. – Barmar Jul 05 '17 at 23:30
  • What's the actual error that PHP gives you? – Mike Jul 05 '17 at 23:30
  • "so I gave that user full sudo permissions without a password" - this is never the way to go about things! – Bytewave Jul 06 '17 at 00:05
  • Sounds like you need to set some environment variables in the PHP script, similar to what you probably do in your `.profile`. – Barmar Jul 06 '17 at 00:29
  • @Barmar I don't need to since it's in the directory that it's being executed from... I'm simply doing `./googler `. Is it an issue with the Python script? – Jamsheed Mistri Jul 06 '17 at 00:54
  • 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()`. – Barmar Jul 06 '17 at 01:00

2 Answers2

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
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.

Joshua Jones
  • 1,364
  • 1
  • 9
  • 15