PHP code example of larrythecoder / grpc

1. Go to this page and download the library: Download larrythecoder/grpc 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/ */

    

larrythecoder / grpc example snippets


Call::drainCompletionQueue(PHP_INT_MIN);

import Grpc\Status\RpcCallStatus;

$request = $service->startUnaryCall(new Parameter()); // Method generated by grpc
$request->onStreamNext(function (Message $message, RpcCallStatus $status): void {
    // Perform operations on $message, will be called once.
})

import Grpc\Status\RpcCallStatus;

$request = $service->startServerStreamingCall(new Parameter()); // Method generated by grpc
$request->onStreamCompleted(function (RpcCallStatus $status): void {
    // Indicates that this stream is complete.
});
$request->onStreamNext(function (Message $message): void {
    // Perform operations on $message, will be called multiple times.
});

import Grpc\Status\RpcCallStatus;

$request = $service->startServerStreamingCall(new Parameter()); // Method generated by grpc
$request->onStreamCompleted(function (RpcCallStatus $status): void {
    // Indicates that this stream is complete.
});

$consumer = function (Message $message): void {
    // Perform operations on $message, will be called multiple times.
};

// This is an example, the behavior depends on your 

import Grpc\Status\RpcCallStatus;

$data = []; // Data that contains N values.

$request = $service->startClientStreamingCall(); // Method generated by grpc

// It is important to note that you MUST wait for the client to finish sending the initial metadata to the server,
// which is why we wait for a callback from them, then after that we can finally start sending the messages to
// the server. ALTHOUGH, for synchronous operation, this note does not apply because the initial metadata was
// already sent during the start of the RPC.
$request->onClientReady(function () use ($request, $data): void {
    while (!empty($data)) {
        $message = array_pop($data);
        $request->onClientNext($message);
    }
    
    // After sending all the necessary messages, be sure to call this method so that the server knows that
    // you are not sending any messages thus returning the result of processing the given messages from the server.
    $request->onClientCompleted(function (Message $message, RpcCallStatus $status) {
        // Handle $message consumed from the server.
    })
});

import Grpc\Status\RpcCallStatus;

$request = $this->gsmsService->startBidiStreamingCall();

// Wait for the initial metadata to be sent, for synchronous operations, this does not apply and can directly
// call the onStreamNext method.
$request->onClientReady(function () use ($request): void {

    // For synchronous requests, you are )) {
    $request->onClientNext(new Message()); // Send message.
}