PHP code example of jameswatts / cake-jsonrpc

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

    

jameswatts / cake-jsonrpc example snippets


CakePlugin::load('Jsonrpc');

public $components = array(
	'Jsonrpc.Server' => array(
		'listen' => array('example') // will process JSON-RPC requests sent to this action
	)
);

public function user($request) {
	if (isset($request->params->userId)) {
		return $this->User->findById($request->params->userId);
	} else {
		throw new Exception('No user ID was specified', 123);
	}
);

public $components = array('Jsonrpc.Client');

public function getUser() {
	// create a JSON request object
	$request = $this->Client->createJsonRequest('user', array('userId' => 7));
	// send the request to the server and return the result
	return $this->Client->sendJsonRequest($request, array('host' => 'example.com', 'path' => '/api/call'));
);

public function getAllTheThings() {
	// create multiple JSON request objects in an array
	$batch = array(
		$this->Client->createJsonRequest('hat', array('hatId' => 11)),
		$this->Client->createJsonRequest('jacket', array('jacketId' => 55)),
		$this->Client->createJsonRequest('shoes', array('shoesId' => 73))
	);
	// send the array of requests to the server and return the results as an array
	return $this->Client->sendJsonRequest($batch);
);