The RestClient
class serves as the entry point to this API. Create a new instance of it with the REST service's endpoint URL and then invoke its execute()
method to work with the resources.
Here's an overview of the typical usage for working with the REST API.
use webservices\rest\RestClient;
use webservices\rest\RestRequest;
use peer\http\HttpConstants;
$client= new RestClient('http://api.example.com/');
$request= (new RestRequest('/resource/{id}'))
->withMethod(HttpConstants::GET)
->withSegment('id', 5000)
->withParameter('details', 'true')
->withHeader('X-Binford', '6100 (more power)'
;
$response= $client->execute($request);
$content= $response->content(); // Raw data as string
$value= $response->data(); // Deserialize to map
The REST API supports automatic result deserialization by passing a lang.Type
instance to the data()
method.
$resource= $response->data('com.example.api.types.Person');
Basic authentication is supported by embedding the credentials in the endpoint URL:
use webservices\rest\RestClient;
$client= new RestClient('http://user:pass@api.example.com/');