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 ?

7
  • What operating system are you using? Commented Oct 29, 2014 at 15:36
  • Please paste the full code you're using! Commented Oct 29, 2014 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 Commented Oct 29, 2014 at 15:43
  • Edited my original post. Commented Oct 29, 2014 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). Commented Oct 29, 2014 at 16:02

2 Answers 2

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.

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

Comments

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

Comments

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.