简体   繁体   中英

Using PHP-FPM inside a PHP process

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM