1. Go to this page and download the library: Download xp-forge/rest-client 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/ */
xp-forge / rest-client example snippets
use webservices\rest\Endpoint;
$api= new Endpoint('https://api.example.com/');
$result= $api->resource('users')->post(['name' => 'Test'], 'application/json');
// Get location from created response, raising an UnexpectedStatus
// exception for any statuscode outside of the range 200-299.
$url= $result->location();
// Same as above, but handle 201 *AND* 200 status codes - see
// https://stackoverflow.com/questions/1860645
$id= $result->match([
200 => fn($r) => $r->value()['id'],
201 => fn($r) => (int)basename($r->location())
]);
use webservices\rest\Endpoint;
$api= new Endpoint('https://api.example.com/');
// Test for existance with HEAD, raising UnexpectedStatus exceptions
// for any status code other than 200 and 404.
$exists= $api->resource('users/1549')->head()->match([
200 => true,
404 => false
]);
// Return user object, raising an UnexpectedStatus exception for any
// statuscode outside of the range 200-299.
$user= $api->resource('users/self')->get()->value();
// Same as above, but returns NULL for 404s instead of an exception
$user= $api->resource('users/{0}', [$id])->get()->optional();
// Pass parameters
$list= $api->resource('user')->get(['page' => 1, 'per_page' => 50])->value();
// Access pagination via `Link: <...>; rel="next"` header
$resource= 'groups';
do {
$result= $this->endpoint->resource($resource)->get(['per_page' => 200]);
foreach ($result->value() as $group) {
yield $group['id'] => $group;
}
} while ($resource= $result->link('next'));
use webservices\rest\Endpoint;
$api= new Endpoint('https://api.example.com/');
$resource= $api->resource('users/self')
->sending('application/json')
->accepting('application/json')
;
// Default content type and accept types set on resource used
$updated= $resource->put(['name' => 'Tested', 'login' => $mail])->value();
// Resources can be reused!
$updated= $resource->patch(['name' => 'Changed'])->value();
use webservices\rest\Endpoint;
$api= new Endpoint('https://api.example.com/');
// Pass segments, map 204 to true, 404 to null, raise UnexpectedStatus
// exception otherwise
$api->resource('users/{id}', $user)->delete()->match([
204 => true,
404 => null
]);
use io\File;
use io\streams\MemoryInputStream;
use webservices\rest\Endpoint;
$stream= new MemoryInputStream('Hello');
$file= new File(...);
$endpoint= new Endpoint($url);
$result= $endpoint->resource('files')->upload()
->pass('tc', 'accepted')
->transfer('letter', $stream, 'letter.txt', 'text/plain')
->transfer('cv', $file->in(), $file->filename)
->finish()
;
use com\example\api\types\User;
$result= $api->resource('users/{0}', [$id])->get();
// If a type is passed, the result will be unmarshalled to an object
$map= $result->value();
$object= $result->value(User::class);
// Same for optional, but map and object will be NULL for 404s
$map= $result->optional();
$object= $result->optional(User::class);
// Works with any type from the XP typesystem, e.g. arrays of objects
$list= $api->resource('users')->get()->value('org.example.User[]');
use webservices\rest\UnexpectedStatus;
use util\cmd\Console;
// In unexpected cases
try {
$user= $api->resource('users/self')->get()->value();
} catch (UnexpectedStatus $e) {
Console::writeLine('Unexpected ', $e->status(), ': ', $e->reason());
}
// More graceful handling
$result= $api->resource('users/self')->get();
if ($error= $result->error()) {
Console::writeLine('Unexpected ', $result->status(), ': ', $error);
} else {
$user= $result->value();
}
use webservices\rest\Endpoint;
$api= new Endpoint('https://user:[email protected]/');
use webservices\rest\Endpoint;
$api= new Endpoint('https://[email protected]/');
use webservices\rest\Endpoint;
$api= (new Endpoint('https://api.example.com/'))->with(['X-API-Key' => $key]);
use webservices\rest\Endpoint;
use io\streams\Compression;
// Detect supported compression algorithms and set "Accept-Encoding" accordingly
$endpoint= new Endpoint($api);
// Send "Accept-Encoding: identity", indicating the server should not compress
$endpoint= (new Endpoint($api))->compressing(Compression::$NONE);
// Send "Accept-Encoding: gzip, br"
$endpoint= (new Endpoint($api))->compressing(['gzip', 'br']);
// Do not send an "Accept-Encoding" header, i.e. no preference is expressed
$endpoint= (new Endpoint($api))->compressing(null);
use webservices\rest\TestEndpoint;
$endpoint= new TestEndpoint([
'/users/6100' => function($call) {
return $call->respond(200, 'OK', ['Content-Type' => 'application/json'], '{
"id": 6100,
"username": "binford"
}');
},
'POST /users' => function($call) {
return $call->respond(201, 'Created', ['Location' => '/users/6100']);
},
]);
$response= $endpoint->resource('/users/me')->get();
// Responds with HTTP status 200 and the above JSON payload
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.