0

I've read a lot of answers on here regarding this but I still haven't been able to figure it out. I have some PHP code that is trying to execute a Python script while the PHP also passes the Python some arguments, and then I want the Python to do something and return a value to the PHP.

I've tried it this way:

$url = "URL given";
$retailer = "other string";

$price = system("/usr/local/bin/python3 /full/path/script.py '" . $url . "' '" . $retailer . "'", $retval);
sleep(5);
print "Price: " . $price;

With the Python code (where the function being called prints the value):

def main():
    getPrice(sys.argv[1], sys.argv[2])
main()

And the $price variable doesn't return anything even though if I run the command thats inside system() in Terminal it prints the value out fine.

I've also tried:

exec("/usr/local/bin/python3 /full/path/script.py", $output, $ret_code);
sleep(5);
print "Price: " . $output[0];

But in this case I've tried it the way above where the Python simply prints "hi" instead of passing the arguments through and I still get nothing, as well as trying it the way with passing the args in from earlier.

I can't tell if maybe the Python isn't being called, or perhaps it is and it returns nothing for some unknown reason but if anyone may know the issue here I would love to hear your thoughts

Blake
  • 35
  • 2
  • 1
    Did you check your PHP logs or get [php to display errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)? It is possible that your host disabled `system` calls for security reasons. It also appears you're putting user input directly into `system()` without escaping the arguments. – Mike May 10 '18 at 01:52
  • 1
    Also, according to [a comment on the docs](http://php.net/manual/en/function.system.php#42567), when a return value parameter is provided, it is executed synchronously (i.e. PHP will wait for it to finish before continuing), so there's no need to do `sleep(5)`. – Mike May 10 '18 at 01:57
  • @Mike I just had PHP display errors now and it says "Notice: Undefined offset: 0 in on line 30" on the line that prints "Price: " and the output. What does this mean? Also, my PHP file is never accessed by any user or user inputs, they are variables I've retrieved from my database so I shouldn't need to escape the args, correct? – Blake May 10 '18 at 02:01
  • @Mike and that error is only shown when I do it the second way, not the first – Blake May 10 '18 at 02:04
  • That means that `$output[0]` is undefined. I'm assuming the output was empty. And yes, you should **always** escape anything going into system program execution commands. Just because it comes from your database doesn't mean it's safe for use. – Mike May 10 '18 at 02:06
  • @Mike alright so I'm still stumped on why this may not be working correctly even when I simply have the Python script output a string and doing nothing else. How can I check if my host disable system calls? I'm working on a university Linux machine and so I feel like they could definitely have some sort of security guard like that – Blake May 10 '18 at 02:20
  • What is the value of `$retval`? – Mike May 10 '18 at 02:23
  • @Mike $retval is 1 – Blake May 10 '18 at 02:25
  • So something went wrong with the script: http://tldp.org/LDP/abs/html/exitcodes.html – Mike May 10 '18 at 02:27
  • Anything other than `0` means there was an error. Try executing the script by the command line and see if you can reproduce the error. – Mike May 10 '18 at 02:28
  • @Mike I finally got it to work but the reason behind this is because its something to do with the import lines at the top of my file. I'm importing requests, beautifulsoup4 and sys, but these work fine if I call the script from the command line. What would be the reason behind these not working when called from PHP? – Blake May 10 '18 at 02:39
  • No idea. I don't know Python. I suggest you submit a new question about it. – Mike May 10 '18 at 17:18

0 Answers0