Can python and php work together?

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?

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?.

1

Jay’s answer is correct that you can do this thing.

The normal way to describe this is that you’ll be developing the python algorithm (function) into a “service” (standalone application) that your PHP app will “consume” (make calls to). This is a fine idea if the algorithm is complex enough that you want to think of it as its own program; sequestering your code into separate parts at various levels (separate objects, separate libraries, separate servers) is often a good idea, provided you have clean (defined) separation of concerns.

If you do go this route, HTTP(S) is a good option. The other easy synchronous option would be to use system calls, either to the python script directly or to a daemon wrapping the script. This may have performance advantages, and may be the fastest solution to write, but HTTPS is going to have big flexibility and security advantages.

If an asynchronous option will work I might suggest that. Your php app will leave a record of work to be done in some shared location (like the database), and your python app will be constantly polling* for work that needs to be done. Meanwhile, your php app will be checking for, and using the results of, finished work.
Asynchronous solutions like this are a little tricky to get right, but can be really nice once you have them.

(*If you’re on a big cloud provider like AWS or Azure you may have better options than a poll.)

All that said, be mindful of the objections of your developers!
The decision to break one app into multiple apps shouldn’t be taken lightly. There are a lot of reasons to split a system into simpler independent parts, but the fact that a contractor happened to write their piece in the “wrong” language isn’t one of them.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật