PHP code example of inferno-code / jsonrpc2-php

1. Go to this page and download the library: Download inferno-code/jsonrpc2-php 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/ */

    

inferno-code / jsonrpc2-php example snippets


$obj = new \JSONRPC2\RemoteProxyObject(

	new \JSONRPC2\Transports\HTTP(

		// URL of endpoint
		'https://example.com/some/endpoint',

		// options
		(object) [
			'headers' => [
				'Authorization' => 'Bearer ...'
			]
		]
	)
);

printf("result = %s\n", $obj->substract(50, 23));

$server = new \JSONRPC2\ServerObject();

$server->on(
	'substract',
	[ 'minuend', 'subtrahend' ],
	function ($minuend, $subtrahend) {
		return $minuend - $subtrahend;
	}
);

$headers = [
	'Content-Type: application/json; charset=utf-8',
	'Access-Control-Allow-Origin: *',
	'Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS',
	'Access-Control-Allow-Headers: Authorization, Origin, Content-Type, Accept',
	'Access-Control-Max-Age: 5',
];

foreach ($headers as $header)
	header($header);

$encodedRequest = file_get_contents('php://input');
$encodedResponse = $server->reply($encodedRequest);

echo $encodedResponse;