PHP code example of amphp / rpc

1. Go to this page and download the library: Download amphp/rpc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

amphp / rpc example snippets


 declare(strict_types=1);

orHandler;
use Amp\Http\Server\SocketHttpServer;
use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use Amp\Rpc\Server\RpcRequestHandler;
use Amp\Serialization\NativeSerializer;
use Amp\Socket;
use Monolog\Logger;
use function Amp\async;
use function Amp\ByteStream\getStdout;
use function Amp\Future\await;
use function Amp\Rpc\Examples\createRegistry;
use function Amp\trapSignal;

$logHandler = new StreamHandler(getStdout());
$logHandler->setFormatter(new ConsoleFormatter);
$logger = new Logger('server');
$logger->pushHandler($logHandler);

$registry = new RpcRegistry();
$registry->register(TimeService::class, new class($id) implements TimeService {
    public function getCurrentTime(): float
    {
        return now();
    }
});

$serializer = new NativeSerializer;

$server = SocketHttpServer::createForDirectAccess($logger);
$server->expose('0.0.0.0:1337');
$server->expose('[::]:1337');
$server->start(new RpcRequestHandler($serializer, $registry), new DefaultErrorHandler());

// Stop the server when SIGINT is received (this is technically optional, but it is best to call Server::stop()).
trapSignal(\SIGINT);

$server->stop();

 declare(strict_types=1);

use Amp\Log\StreamHandler;
use Amp\Rpc\Client\RpcClient;
use Amp\Rpc\Examples\Basic\TimeService;
use Amp\Rpc\Interceptor\RoundRobinBalancer;
use Amp\Serialization\NativeSerializer;
use Monolog\Logger;
use ProxyManager\Factory\RemoteObjectFactory;
use function Amp\ByteStream\getStdout;

$logHandler = new StreamHandler(getStdout());
$logHandler->setFormatter(new ConsoleFormatter);
$logger = new Logger('client');
$logger->pushHandler($logHandler);

$serializer = new NativeSerializer;

$proxyFactory = new RemoteObjectFactory(new RoundRobinBalancer([
    new RpcClient('http://localhost:1337/', $serializer),
]));

/** @var TimeService $timeService */
$timeService = $proxyFactory->createProxy(TimeService::class);

// This is a remote call via HTTP
var_dump($timeService->getCurrentTime());