Php – Can python and php work together

appiosiphonePHPpython

I am having a mobile app created for ios. The developers built the app in php. The app requires an algorithm so I found another programmer to develop it. The algorithm programmer built the algorithm in python. The developers refuse to finish the app because they say it won't work with python, while the programmer insist it will. The programmer says put the algorithm in its on server and connect then over http. Will this work and I'd so how risky is it to future problems?

Best Answer

Will this work and I'd so how risky is it to future problems?

Yes it will work, and how risky it is depends on how good your implementation is. This is perfectly acceptable if done correctly. I have successfully integrated PHP and C, when PHP was simply too slow to do certain niche tasks in real time (IIRC, PHP is 7 times slower than its C counterpart).

The best implementation is to ignore the fact they are PHP/Python specifically, and just treat them as two separate languages.

Now, you need to pick some 'standard' way of interacting. HTTP is probably the easiest: everyone has a web server setup already so it's just a matter of putting the script online.

From here, you need to get away from the fact they are PHP/Python still and design your API without knowing that. For example, imagine you had a calculator:

<?php
print '<answer>' . ($_GET['one'] + $_GET['two']) . '</answer>';
?>

Then your API would be http://location/script.php?one=5&two=7. This is perfectly acceptable: the user of the API expects an answer within an <answer> tag (my example is a lazy mans XML, purely as a demonstration, but I think you should look into outputting JSON). This, is not:

<?php
print eval($_GET['expr']);
?>

Since your API would be http://location/script.php?expr=5+7. Even though it would result in the same answer (and, ignoring any major security flaws), the caller needs to know the implementation details of PHP (eval is a PHP function which will just evaluate its input as real PHP code). If they did 2^3, is that 2 xor 3 or 2 * 2 * 2 (2 to the power 3). This is a bad design: your inputs should be independent of the language your API is implemented in. On top of this, how do we distinguish between an error message and an answer? No magic "if the response is not a number", please.

Python side - you just need to make sure HTTP requests fail gracefully. If you can't connect to your script, perhaps try again, and if that fails, give up with a nice error message asking them to try again later. Don't just display them a Python stack trace.

You should also document your API as well as possible - this will save you a lot of time down the line. Say what each of the inputs should be, what ranges they should fall in, and give some good examples of how to use it.

API side - you should also try to fail quickly if something is wrong. If a variable is missing, incomplete, or invalid, print a message and exit as the first thing you do. Do not just run with the values, and then let them get back a PHP error message (remember, they should not know or care what language your API is implemented in), but should get a nice friendly You didn't give me a 'two' variable. What do you want me to add 'one' to?.

Related Topic