I need to make calls from webpage to external library written in C++ and display the result. Platform is Linux, Apache, PHP.
My current idea is to use PHP service which will call my library/program. I found that there are two possible ways to do this:
1) use PHP ‘exec’ function
2) write PHP extension
I am curious what works more effective? Faster? Less load the server?
I will probably need to do 4 calls per second, so I want to be as optimal as possible.
P.S. If you are aware of some other (more effective) way of calling C++ library or program from webpage, please let me know.
Thanks a lot,
Robusta
An extension is theoretically faster because it avoids the overhead of creating a new process. It’s also a “cleaner” solution (no awkward program arguments escaping; you are able to parse arbitrary PHP values such as objects instead of only strings, etc.).
However, if you already have a command line program that uses that library, it will be easier for you to just execute it instead of writing an extension.
Note that if you only make 4 calls per second, performance-wise it’s indifferent which method you use, unless your library requires expensive initialization that can be avoided by having persistent (cross-request) state stored in a PHP extension.