D
D
dhose2017-12-13 03:01:43
PHP
dhose, 2017-12-13 03:01:43

How to organize data exchange between PHP applications?

Hi all!
It is necessary to organize data exchange between two PHP applications, there are sooo many requests (100 requests per second), while their volume is very small - it is important to organize it in such a way that it would work at the speed of light.
In general, there are two PHP applications - you need to make a super fast exchange of small commands between them, like go; stop; read and so on
now it's all implemented using the usual http get, can anyone have any ideas how to speed it up?
Application 1 is based on Windows, running a PHP script from the command line, in which the command request is looped:

//циклим бесконечность
while (true){
  
  //циклим до получения команды
  while (!$command){

       //запрашиваем команду
       $command=file_get_contents("http://site.com/command.php");
   }

   //обрабатываем команду
  if(command=="get_app_status"){

     ....... делаем некое действие соответсвующее команде (читаем txt файл)
     $result=$text;
     
    //возвращаем результат
     echo $result;

  }

}

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Khvorostov, 2017-12-13
@Hakujin

Well, you could open a socket on the daemon and listen to it, and from the second application, drop connections accordingly.
This will be faster in any way than using a web server via http.

A
Alexander, 2017-12-13
@Minifets

Alternatively, you can implement a message queue service.
Gearman for example.

O
Oleg As, 2017-12-13
@eXcNightRider

There is an excellent WAMP socket implementation in PHP called Ratchet. Detailed documentation on this site
And now a recipe from practice:
1. Building a simple application using composer, we need the cboden / ratchet package
2. Writing the base class

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Msg implements MessageComponentInterface {
    public function onOpen(ConnectionInterface $conn) {
    }

    public function onMessage(ConnectionInterface $from, $msg) {
    }

    public function onClose(ConnectionInterface $conn) {
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}

The four functions defined in the code are mandatory, they are designed to handle the corresponding events
3. PHP-CLI script that will start the socket server and receive messages:
msg-server.php
<?php
use Ratchet\Server\IoServer;
use MyApp\Msg;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $server = IoServer::factory(
        new Msg(),
        8080
    );

    $server->run();

If you run this file with the command , the
server will work and you can even connect to it using telnet
4. The client part is the ratchet / pawl
5 package. Implementation example:
<?php

    require __DIR__ . '/vendor/autoload.php';

    \Ratchet\Client\connect('ws://192.168.1.100:8080')->then(function($conn) {
        $conn->on('message', function($msg) use ($conn) {
            echo "Received: {$msg}\n";
            $conn->close();
        });

        $conn->send('Hello World!');
    }, function ($e) {
        echo "Could not connect: {$e->getMessage()}\n";
    });

You can use traditional JSON to pass structured data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question