-
Notifications
You must be signed in to change notification settings - Fork 29
Advanced functionality
The library is designed to be flexible. It allows you to substitute the built-in transport mechanism for sending and receiving requests with one of your own. The following transports are used by default:
- the client calls
file_get_contentsusing a stream-context - the server reads
php://input
If you find these mechanisms too limiting then you can provide your own by passing them as the second parameter of the JsonRpc\Client and JsonRpc\Server constructors, as described below. Note that you can override the default server mechanism by passing your input to the $server->receive function.
For reference you can see the default class at src/JsonRpc/Transport/BasicClient.php. Your transport class must be instantiable and must contain the following:
- a
$transport->sendpublic function returning true or false - a
$transport->outputpublic property - a
$transport->errorpublic property
It should look like this:
<?php
class MyClientTransport
{
public $output = '';
public $error = '';
public function send($method, $url, $data, $headers = array())
{
// do stuff to send $data and get the response
...
if ($response)
{
// put the response in $this->output property
$this->output = $response;
return true;
}
else
{
// put the error in $this->error property
$this->error = $error;
return false;
}
}
}To use your class, create it and pass it to the client constructor:
<?php
$transport = new MyClientTransport();
$client = new JsonRpc\Client($url, $transport);Alternatively you can use the client->setTransport method:
<?php
$client = new JsonRpc\Client($url);
$transport = new MyClientTransport();
$client->setTransport($transport);Your transport class must be instantiable and must contain the following:
- a
$transport->receivepublic function that returns the data received - a
$transport->sendpublic function that sends the data
It should look like this.