36

For some reason, I have to run a php script to get an image from Python. Because the php script is very big and it is not mine, it will takes me days to find out the right algorithm used and translate it into python.

I am wonder if there is any way to run php script, with few parameters, which returns a image, in python.

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
jiahao
  • 3,373
  • 2
  • 35
  • 36

5 Answers5

49

Example code:

import subprocess

# if the script don't need output.
subprocess.call("php /path/to/your/script.php")

# if you want output
proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • 4
    This is the code pasted from http://mail.python.org/pipermail/tutor/2009-October/071973.html – Alvin Jul 29 '12 at 19:22
  • 13
    it is incorrect. The first call won't work on POSIX systems. Use `subprocess.call(["php", "/path/to/your/script.php"])` instead. Use `script_response = subprocess.check_output(["php", "/path/to/your/script.php"])` instead of the second example. – jfs Oct 29 '14 at 16:34
  • 1
    if the argument `shell` is set to `True` then using the string rather than a list is ok – Valen Jul 22 '17 at 07:06
  • 1
    Another way is: os.system("php /path/to/your/file.php") – Akshay Lokur Oct 23 '18 at 05:39
16

You can simply execute the php executable from Python.

Edit: example for Python 3.5 and higher using subprocess.run:

import subprocess

result = subprocess.run(
    ['php', 'image.php'],    # program and arguments
    stdout=subprocess.PIPE,  # capture stdout
    check=True               # raise exception if program fails
)
print(result.stdout)         # result.stdout contains a byte-string
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • 1
    Assuming the php script can be called from the command line and accepts a command line argument to describe what to do with the image you could use the built in subprocess package. import subprocess subprocess.call(["/path/to/php", "/path/to/php/script.php", "argument"]) – Rob Young Jan 24 '12 at 09:26
  • 1
    Post an example – tumbleweed Mar 06 '17 at 15:01
7

You can use php.py. This would allow you to execute php code in python, like in this example (taken from here):

php = PHP("require '../code/private/common.php';")
code = """for ($i = 1; $i <= 10; $i++) { echo "$i\n"; }"""
print php.get_raw(code)
Wesley
  • 2,204
  • 15
  • 14
  • Hi. Thanks for your help. If you check the source code of this program, you can see that I have to enter the php code manually. Since it is also linked to some .inc files, it is not so good option. But thanks anyway. – jiahao Jan 24 '12 at 10:32
4

If you can run the PHP script locally from the command-line, subprocess.check_output() will let you can PHP and will capture the return value.

If you are accessing PHP via a socket, then you can use urllib.urlopen() or urllib.urlretrieve() to pull down the resource.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
3

Make a wrapper around the PHP script, which:

  • performs the stuff (If I understand well, it's an image creation),
  • then redirects (301 Moved Permanently) to the result image,
  • (also, the image should be purged some day).

So you can refer to this service (the PHP script) with a simple HTTP request, from anywhere, you can test it with browser, use from Python prg, you need just download the image the usual way.

Also, if you have a such standalone sub-system, don't feel bad about implement it with different language/technique. It has several advantages, e.g. you can install that service on a different host.

Recommended reading: Service-Oriented Architecture on Wikipedia.

ern0
  • 3,074
  • 25
  • 40