(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.
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.
will output a string containing the numbers “1”..“10”
This code would return a Python list with the numbers 1..10.
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.