PHP code example of bonuscred / rest-client

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

    

bonuscred / rest-client example snippets


$req = new RestClient\Request;
$res = $req->get('https://api.github.com/repos/dnlfranklin/rest-client');

echo $res->getHeaderLine('content-type');
echo $res->get_http_code();
echo $res->get_data(); 

$res = new RestClient\Request::run('https://api.github.com/repos/dnlfranklin/rest-client', 'GET'); // Objeto Response

$req = new RestClient\Request;

// Callback chamado em caso em requisições bem sucedidas
$req->onSuccess(function(Response $response){
    echo 'Success: '.$response->get_http_code();
});

// Callback chamado em caso de erro de requisição
$req->onError(function(Response $response){
    echo 'Error: '.$response->get_errmessage();
});

// Callback chamado após finalização de requisição
$req->onComplete(function(Response $response){
    echo $response->get_body();
});

$res = $req->get('https://api.github.com/repos/dnlfranklin/rest-client');

$req = new RestClient\Request;

$res_data = $req->baseUrl('https://api.github.com')
                ->buildIndexedQueries(true)
                ->requestFormat('json')
                ->responseFormat('json')
                ->userAgent('Minha API/1.0.0')
                ->onError(function(Response $response){
                    echo 'Error: '.$response->get_errmessage();
                })
                ->get('/repos/dnlfranklin/rest-client')
                ->get_data();            

$res = new RestClient\Request::run('https://api.github.com/repos/dnlfranklin/rest-client');

$data = $res->get_data(); // Array de atributos recebidos no formato json

$res = new RestClient\Request::run('https://api.github.com/repos/dnlfranklin/rest-client');

$data = $res->decode(function($body){
    //Tratamento específico de decode
});