1

The setup is two separate PHP projects running on the same server using PHP-FPM. Currently they "speak" to each other over JSON by making HTTP requests with cURL.

The issue here is that cURL and web server overhead is a waste of time. After all the other program is right there, in a folder just above the current one. So why go the long way around with cURL and HTTP? The trick is I can't just include a file form the other project because autoloaders clash and make a big mess. For that reason they need separate processes and not share too much.

What I've proposed to solve the issue is to create a Node.js server that listens to a socket that my PHP process can connect to and is able to make requests to PHP-FPM directly using the node-phpfpm module. While this solves the issue, I'm asking myself why is that Node.js proxy needed?

There must be a way to make a new FPM request directly from PHP but I haven't found it.

I know I could also use the CLI executable with exec() but that's not pretty at all. To be more specific, passing request data with exec() is nearly impossible.

1
  • Without a bunch of work making HTTP requests is going to be the most efficient. Adding a Node proxy doesn't really do that much for you, after all if you're using PHP-FPM it's probably already fronted with a reverse proxy like Nginx. Commented Mar 19, 2016 at 19:56

1 Answer 1

1

You can make a request from PHP script to PHP-FPM instance directly through UNIX or TCP/IP socket using, for example, this library: https://github.com/ebernhardson/fastcgi

Example code, based on readme:

<?php

use EBernhardson\FastCGI\Client as FastCGIClient;
use EBernhardson\FastCGI\CommunicationException;

$client = new FastCGIClient('/var/run/php5-fpm.sock');

try {
    $client->request([
        'REQUEST_METHOD'  => 'POST',
        'SCRIPT_FILENAME' => '/full/path/to/script.php',
    ], $postBody);
    $response = $client->response();
} catch (CommunicationException $e) {
    // Handle exception
}

There are other libraries you can consider: https://packagist.org/search/?q=fastcgi

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I actually found a clone of the library you mentioned soon after posting. An extension would be much nicer though. I don't like the idea of specifying how to connect, that is done on the web server side. Duplicating configuration always causes trouble.
Seems like this is an only option, because PHP-FPM SAPI does not expose much API functions, like Apache module does. For example, script running in mod_php can make internal subrequest using this function php.net/manual/en/function.virtual.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.