0

I have a REST API backed by PHP, that calls a C program. Depending on the inputs, the C program returns JSON data, and the PHP reads the output and directly sends it back to the client.

The code is essentially:

$command = $binary . " " . $parameters;
$output = shell_exec($command);
echo $output;

For each invocation of the API, PHP starts up the program, does some processing, and ends.

I would like to make this more efficient by making the C program run as a daemon, and then have the PHP backend call into this daemon.

Also, I might be migrating to a Python web framework, so I would like something that is cross-platform from the client side.

My gut reaction is to simply do it over sockets, since then I can put the daemon anywhere (it would for the foreseeable future be located on the same web server, but I could move it off eventually). But is sockets efficient enough, or is there something else that is considered a better practice?

steve8918
  • 1,820
  • 6
  • 27
  • 38
  • 1
    I've been using Gearman recently, and it'll do this no problem. Although primarily it is a queue, you can also kick off blocking workers if you want to wait in PHP until something is done. I believe workers can be written in most languages - whilst I'm using PHP again, I should think C would be no problem. – halfer Apr 17 '12 at 21:20
  • 1
    Cross platform, Cross language, distributed, fast, nice: http://www.zeromq.org/intro:read-the-manual – hakre Apr 17 '12 at 21:23
  • possible duplicate of http://stackoverflow.com/questions/4789720/communicate-c-program-and-php/ – Bernardo Ramos Apr 15 '17 at 02:45

1 Answers1

1

It's hard to answer this without knowing more about your C program.

One thing which is definitely worth checking is creating a PHP extension.

It's not cross platform, but you can write similar extensions to Python or Ruby, and the extension part is really just a small skeleton around your C program, and it's really easy to write.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • Thanks, my program is really just a small C program that returns strings based on user input. So it's not very complex yet. I will likely investigate zeromq though, that might be more what I'm looking for. – steve8918 Apr 18 '12 at 02:40
  • Link for "creating a PHP extension" (http://theserverpages.com/php/manual/en/zend.creating.php) **with 404 error**. – Peter Krauss Nov 12 '12 at 09:31