Brool brool (n.) : a low roar; a deep murmur or humming

Hacks: Python Calling PHP

| python bridge php coding hacks

(This is almost too stupid to post, but on the off chance that someone actually needs something like this…)

I needed to interface with a bunch of data that had PHP wrapper classes, and needed a quick way of being able to interface with PHP from Python. (At this point, you might find it hard to believe that the PHP wrapper classes were worth trying to reuse, but the wrapper classes took care of partitioning and caching and everything, so it seemed silly to not reuse them). I considered using the PHP API and ctypes to make a Python to PHP bridge , but then decided that was entirely too much work, so I just hacked together a stupid class that invoked PHP with arbitrary code and returned it in a number of ways.

Basic usage

Create the PHP class (giving it an optional prefix and postfix – prefix is the most useful, as it allows you to specify requires), and then invoke a code block with get_raw, get, or get_one. Note that every “call” into PHP will create and destroy a PHP process.

Some examples:

Reading raw input

php = PHP("require '../code/private/common.php';")
code = """for ($i = 1; $i <= 10; $i++) { echo "$i\n"; }"""
print php.get_raw(code)

will output a string containing the numbers “1”..”10”

Reading one big JSON value

php = PHP("require '../code/private/common.php';")
code = """                                                                                          
$a = array();                                                                                       
for ($i = 1; $i <= 10; $i++) { $a[] = $i; }                                                         
echo json_encode($a);"""
print php.get(code)

This code would return a Python list with the numbers 1..10.

Reading many JSON values

    php = PHP("require '../code/private/common.php';")
    code = """                                                                                          
    for ($i = 1; $i <= 10; $i++) {                                                                      
        echo json_encode(array($i => $i * $i)) . "\n";                                                  
    for row in php.get_one(code):
        print row

Given a PHP snippet that returns one JSON value per line, iterate through them as Python values.

Find it on Github.


Comments are moderated whenever I remember that I have a blog.

Drucik | July 28, 2009
Hi! As you wrote, someone may needs something like this. For me it worked (only warning about popen2 being obsolete) and was very helpful :)
toudi | June 15, 2010
This is absolutely awesome!!! Thanks for this snippet :D
Stan | September 21, 2014
line 16 of should be (out, inp) = popen2.popen2("php", mode='b') # using python 2.7.8 Setting the mode explicitly fixes an error that arises when data over 4KB through the php.pq class function call to popen2. Also, python text sent to a php function needs to escape the single quote, e.g., str = str.replace("\'", "\\'") Other than that, it's brilliant. Thank You.
Add a comment