Today, we are going to look at a very useful tool for making computations with your data on the server.
Why we need this article:
PHP is great for communicating with your database, especially creating new data, updating existing data, retrieving data and removing it. However, it is not suitable for making computations based on your data before displaying it to the user. Consider, for example, a web application that receives regular (every minute) GPS coordinates from your mobile phone as you go about each day. Later, when you visit the web application, you want to see how far you traveled by road on July 1st, 2014. Now you might be able to just grab a list of that huge set of coordinates for July 1st via your method of retrieving data from your database in PHP, but the thought of matching it up to the coordinates that constitute all roads in your vicinity is a much more daunting task. We can use more of your server's computational ability by using Python.
I don't want to spend a long time explaining why you should perform complex computations in Python instead of PHP. Let's get straight into how.
In PHP, you will need a function called exec(). This function can execute an external program on your server. On your server, you will need Python and a worthy script.
Good communication practices:
exec() takes an argument which is the php variable into which the output of your program will be placed. Unfortunately, this means that it will be an array of strings. So for every print() statement in your Python script, you will get one element of your string array in PHP.
For example:
exec("python get_coords.py", $data);
$data will come out of this line containing one element for every line you print() in Python.
So if you know that it all comes down to communicating via strings, we have to decided how we're going to appropriately return our data from our Python script. Json sounds like a perfect solution. Import the json module into your py program:
import json
Python has some easy to understand data structures to build your json output with, dicts and lists. Here is a refresher on the two:
# ca = []
# ce = [] # both of these will be lists
ca = get_afternoon_coordinates()
ce = get_evening_coordinates()
coords = { 'afternoon' : ca, 'evening': ce }
As you can see, you can create a pretty nifty nested hierarchy of lists and dictionaries that will translate easily into Json.
When your python program successfully builds this structure, you will want to use the json module to build a json string out of the structure. Then you'll print it as output.
print(json.dumps(coords))
The above line wraps up the responsibility of your Python program to create good data for PHP to parse.
In PHP you will then have access to this as the first element of the array coming back from the exec() command:
$jdata = json_decode($data[0], true);
json_decode is a PHP function that will take a json string and return a PHP variable. Optionally, you will want to specify the 2nd argument, true, which is whether you want the objects return to be associative arrays.
If your outer json structure constructed by your python program was a dictionary, you will now have access to the values via $jdata['mykey'].
Conclusion:
When your python program successfully builds this structure, you will want to use the json module to build a json string out of the structure. Then you'll print it as output.
print(json.dumps(coords))
The above line wraps up the responsibility of your Python program to create good data for PHP to parse.
In PHP you will then have access to this as the first element of the array coming back from the exec() command:
$jdata = json_decode($data[0], true);
json_decode is a PHP function that will take a json string and return a PHP variable. Optionally, you will want to specify the 2nd argument, true, which is whether you want the objects return to be associative arrays.
If your outer json structure constructed by your python program was a dictionary, you will now have access to the values via $jdata['mykey'].
Conclusion:
- PHP is awesome and can do a lot.
- Python is awesome and can do a lot.
- Together they can do amazing things that each one couldn't do alone.
- Use JSON.
Please feel free to use my techniques to smartly write web applications that use PHP and Python.
