0

I need to call a PHP script from within python. I found this thread on StackOverflow:

Execute php code in Python

So I use this exact method to call my php script. However, I get the following error:

php is not recognised as an internal or external command

First I figured it was because php wasn't in my PATH environment variable, so added it and it worked in my command line, but my Python script kept returning the same error. I also tried adding php to my PYTHONPATH, with the same result. I finally even tried to put the php file in the folder where the php executable is located, but this also didn't work.

What do I need to change in order for this to work?

EDIT

My operating system is Windows 7. This is the code I'm using:

proc = subprocess.Popen("php C:/xampp/htdocs/test/test.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()

I didn't try to run my python script from my command line, which does not produce the error and executes the php script. The script_response variable is empty however.

This is the last line of my test.php file:

return $aResult;

I checked the $aResult, it does contain a value so it's not returning properly. Do I need something else in my php script instead of return ?

Community
  • 1
  • 1
SergioP
  • 241
  • 2
  • 4
  • 14
  • What operating system are you using? – Jerodev Oct 29 '14 at 15:36
  • Please paste the full code you're using! – motanelu Oct 29 '14 at 15:42
  • (i suppose that you are under Microsoft operating system) Add PHP to your environment variable permanently, then start a new command line and launch your python script – Halayem Anis Oct 29 '14 at 15:43
  • Edited my original post. – SergioP Oct 29 '14 at 15:58
  • 1
    `return` in a PHP process will go to the bit-bucket, because there's no PHP process to read that internal stream. If you need to read it with something else (in this case: Python), send it to its `STDOUT` (in other words: `echo` or `print` the value). – Wrikken Oct 29 '14 at 16:02
  • I'm returning an array. `print` or `echo` returns an error. `print_r()` returns a string, which makes processing the data much harder. Is there a way to return the array as an array, not as a string? – SergioP Oct 29 '14 at 16:08
  • @SergioP: you could use `json` format to exchange data between processes. If you use subprocess then you need to parse strings (byte sequences) there is no way around it. You can hide it in a library in the best case. Please, try to limit the scope of your question: Is it how to run `php` script from Python (it is resolved as I understand)? Or is it about how to pass data between processes (different unrelated issue)? If you have a new question; [ask a new question](http://stackoverflow.com/questions/ask). – jfs Oct 29 '14 at 16:42

2 Answers2

2

You could use json to exchange data between processes. Replace return $result in the php script with:

echo json_encode($result); 

And in Python:

import json
from subprocess import check_output

result = json.loads(check_output(["php", "/path/to/test.php"]).decode('utf-8'))

If php is not found then read this explanation about how Windows searches for executables.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

You may have to use the full path to PHP instead of simply calling php.

If you're using a Unix system, you can find the full path by typing which php into your terminal, and that should return something like /usr/bin/php. On Windows, I think you can type where php.exe to find the full path.

Then in your Python script you can do:

# Run PHP script
command = "/full/path/to/php /path/to/myscript.php"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
response = process.stdout.read()
# Print response from PHP script
print response
seane
  • 589
  • 1
  • 4
  • 15