PHP code example of bit3 / remote-objects

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

    

bit3 / remote-objects example snippets


class RemoteObject
{
	public function reply($name)
	{
		return 'Hello ' . $name;
	}
}

$transport = new RemoteObjects\Transport\UnixSocketServer('/tmp/socket.server');

$encoder = new RemoteObjects\Encode\JsonRpc20Encoder();

$target = new RemoteObject();

$server = new RemoteObjects\Server(
	$transport,
	$encoder,
	$target
);
$server->handle();



$transport = new RemoteObjects\Transport\UnixSocketClient('/tmp/socket.client', '/tmp/socket.server');

$encoder = new RemoteObjects\Encode\JsonRpc20Encoder();

$server = new RemoteObjects\Client(
	$transport,
	$encoder
);
$result = $server->invoke('reply', 'Tristan');
echo $result; // -> "Hello Tristan"

$server = new RemoteObjects\Server(
	$transport,
	$encoder,
	array(
		'a' => $targetA,
		'b' => $targetB
	)
);

$client->invoke('a.methodA'); // invoke $targetA->methodA();
$client->invoke('b.methodB'); // invoke $targetB->methodB();

$server = new RemoteObjects\Server(
	$transport,
	$encoder,
	array(
		'a' => array(
			'one' => $targetOne,
			'two' => array(
				'I' => $targetI,
				'II' => $targetII
			)
		),
		'b' => $targetB
	)
);

$result = $client
	->castAsRemoteObject()
	->a
	->two
	->II
	->method();

class Objects
{
	public function getA()
	{
		return new TargetA();
	}

	public function getB()
	{
		return new TargetB();
	}
}


$server = new RemoteObjects\Server(
	$transport,
	$encoder,
	new Objects()
);

$server = new RemoteObjects\Server(
	$transport,
	$encoder,
	array(
		'a' => $targetA,
		'b' => $targetB
	)
);

$remoteA = $client->getRemoteObject('a');
$remoteB = $client->getRemoteObject('b');

$remote = $client->castAsRemoteObject();

interface MyRemote
{
	public function remoteMethod();
}

$remote = $client->castAsRemoteObject('MyRemote');

class MyRemote
{
	public function remoteMethod()
	{
		// ...
	}
}

$remote = $client->castAsRemoteObject('MyRemote');

$jsonEncoder = new RemoteObjects\Encode\JsonRpc20Encoder();
$encoder = new RemoteObjects\Encode\AesEncoder(
	$jsonEncoder,
	'<add your pre-shared key here>'
);

$jsonEncoder = new RemoteObjects\Encode\JsonRpc20Encoder();
$encoder = new RemoteObjects\Encode\RsaEncoder(
	$jsonEncoder,
	'<add client public key here>',
	'<add server private key here>'
);

$jsonEncoder = new RemoteObjects\Encode\JsonRpc20Encoder();
$encoder = new RemoteObjects\Encode\RsaEncoder(
	$jsonEncoder,
	'<add server public key here>',
	'<add client private key here>'
);

$logger = new \Monolog\Logger('RemoteObjects');
$logger->pushHandler(new StreamHandler('php://stderr', Logger::ERROR));

$transport->setLogger($logger);

$encoder->setLogger($logger);

$server->setLogger($logger);
// or
$client->setLogger($logger);

user -> Client::invoke($method, $params)
         \
          \--> Encoder::encodeMethod($method, $params)
                     /
        $request  <-/
         \
          \--> Transport\Client::request($request)
                |
                |
       HTTP or something else :-)
                |
                V
              Transport\Server::receive()
                     /
        $request  <-/
         \
          \--> Encoder::decodeMethod($request)
                               /
        [$method, $params]  <-/
         \
          \--> Server::invokeArgs($method, $params)
                \
                 \--> $target->$method($params..)
                            /
               $result  <--/
                \
                 \--> Encoder::encodeResult($result)
                             /
               $response <--/
                \
                 \--> Transport\Server:respond($response)
                       |
                       |
                All the way back
                       |
                       V
                      Transport\Client::request(...)
                      /
        $response <--/
         \
          \--> Encoder::decodeResult($response)
                    /
        $result <--/
         /
user <--/