PHP code example of corex / client

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

    

corex / client example snippets


// Get 1 post.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response);

// Get 1 post by token on request.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/{id}');
$request = (new Http\Request())->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

// Get 1 post by path and token on request.
$client = new Http\Client('https://jsonplaceholder.typicode.com');
$request = (new Http\Request())->path('/posts/{id}')->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

// Create request with path and token on request.
$request = new Http\Request();
$request->path('/posts/{id}');
$request->token('id', 1);
var_dump($request);

// Create request with header.
$request = new Http\Request();
$request->header('Accept', 'application/json');
var_dump($request);

// Create request with query parameter fields.
$request = new Http\Request();
$request->param('fields', 'firstname,lastname');
var_dump($request);

// Create request with body set.
$request = new Http\Request();
$request->body('{"something":["test1","test2"]}');
var_dump($request);

// Get body from response.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->body());

// Get status + status-message from response.
$client = new Http\Client('https://jsonplaceholder.typicode.com/unknown');
$response = $client->call(Method::GET);
if ($response->status() != 200) {
    var_dump($response->status());
    var_dump(Status::message($response->status()));
}

// Get response headers.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->headers());

// Get Content-Type from response headers.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->header('Content-Type'));

// Get 1 post.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response);

// Get 1 post by token on request.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/{id}');
$request = (new Rest\Request())->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

// Get 1 post by path and token on request.
$client = new Rest\Client('https://jsonplaceholder.typicode.com');
$request = (new Rest\Request())->path('/posts/{id}')->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

// Create request with path and token on request.
$request = new Rest\Request();
$request->path('/posts/{id}');
$request->token('id', 1);
var_dump($request);

// Create request with header.
$request = new Rest\Request();
$request->header('Accept', 'application/json');
var_dump($request);

// Create request with query parameter fields.
$request = new Rest\Request();
$request->param('fields', 'firstname,lastname');
var_dump($request);

// Create request with fields.
$request = new Rest\Request();
$request->field('firstname', 'Roger');
$request->field('lastname', 'Moore');
var_dump($request);

// Get body from response.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->body());

// Get status + status-message from response.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/unknown');
$response = $client->call(Method::GET);
if ($response->status() != 200) {
    var_dump($response->status());
    var_dump(Status::message($response->status()));
}

// Get response headers.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->headers());

// Get Content-Type from response headers.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->header('Content-Type'));

// Get title from response. Dot notation supported.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->value('title'));

// Get response as array.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->toArray());

class Album extends Entity
{
    public $userId;
    public $id;
    public $title;
}

class Albums extends Collection
{
    public function current()
    {
        return new Album(parent::current());
    }
}

// Using above classes will make you able to iterate over album/albums and have auto-completion.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/albums');
$response = $client->call(Method::GET);
$albums = new Albums($response);
foreach ($albums as $album) {
    print($album->title . "\n");
    var_dump($album->toArray());
}