PHP code example of simukti / rest-client

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

    

simukti / rest-client example snippets



use RestClient\HttpClient\PeclHttpClient;
use RestClient\Request\ApiRequest;
use RestClient\Response\ApiResponse;

$request = new ApiRequest('https://api.github.com');
$request->setPath('/users/simukti/repos')
    ->addQuery('sort', 'updated')
    ->addQuery('type', 'owner')
    ->addHeader('accept', 'application/json');

// default http method is GET
$response   = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);


use RestClient\HttpClient\PeclHttpClient;
use RestClient\Request\ApiRequest;
use RestClient\Response\ApiResponse;

$request = new ApiRequest('https://httpbin.org');
$request->setPath('/post')
    ->setMethod(ApiRequest::METHOD_POST)
    ->addData('username', 'kadalkesit')
    ->addData('password', 'superkesit')
    ->addQuery('foo', 'bar')
    ->addQuery('baz', 'bat');
// application/x-www-form-urlencoded
$response   = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);


use RestClient\HttpClient\PeclHttpClient;
use RestClient\Request\ApiRequest;
use RestClient\Response\ApiResponse;

$request = new ApiRequest('https://httpbin.org');
$request->setPath('/post')
    ->setMethod(ApiRequest::METHOD_POST)
    ->addData('user_id', 100)
    ->addFile('picture', '/path/to/your/file_to_upload.extension');

$oken          = 'your_generated_token';
$authorization = new \RestClient\Authorization\BearerStringAuthorization($githubToken);
$request->setAuthorization($authorization);

$response   = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);


use RestClient\HttpClient\PeclHttpClient;
use RestClient\Request\ApiRequest;
use RestClient\Response\ApiResponse;

$request = new ApiRequest('https://httpbin.org');
$request->setPath('/post')
    ->setMethod(ApiRequest::METHOD_POST)
    ->setDataRaw(json_encode(['key' => 'value1', 'key2' => [ 'subkey2.1' => 'subkey2.1 value'] ]))
    ->addHeader('content-type', 'application/json');

$httpClient = new PeclHttpClient;
$response = $httpClient->send($request, new ApiResponse);


use RestClient\HttpClient\PeclHttpClient;
use RestClient\Request\ApiRequest;
use RestClient\Response\ApiResponse;

$request = new ApiRequest('https://httpbin.org');
$request->setPath('/put')
    ->setMethod(ApiRequest::METHOD_PUT)
    ->addData('username', 'kadalkesit')
    ->addData('password', 'superkesit')
    ->addQuery('foo', 'bar')
    ->addQuery('baz', 'bat')
    ->addHeader('content-type', 'application/json');
// json body
$response   = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);


use RestClient\HttpClient\PeclHttpClient;
use RestClient\Request\ApiRequest;
use RestClient\Response\ApiResponse;

$request = new ApiRequest('https://httpbin.org');
$request->setPath('/delete')
    ->setMethod(ApiRequest::METHOD_DELETE)
    ->addQuery('user_id', 1);

$response   = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);

use RestClient\HttpClient\PeclHttpClient;
use RestClient\Request\ApiRequest;
use RestClient\Response\ApiResponse;

$request = new ApiRequest('https://httpbin.org');
$request->setPath('/post')
    ->setMethod(ApiRequest::METHOD_POST)
    ->setData([
        'username' => 'kadalkesit',
        'password' => 'superkesit'
    ])
    ->addQuery('expand', 'user,role');

$simpleJWT = new \RestClient\Authorization\JWTAuthorization('key_as_ISS', 'secret', [
    'jti' => 'jtid',
    'scope' => [
        'user', 'writer'
    ]
]);
$request->setAuthorization($simpleJWT);

$response   = new ApiResponse;
$httpClient = new PeclHttpClient;
$httpClient->send($request, $response);


use RestClient\HttpClient\PeclHttpClient;
use RestClient\Request\ApiRequest;
use RestClient\Response\ApiResponse;

$request = new ApiRequest('https://httpbin.org');
$request->setPath('/post')
    ->setMethod(ApiRequest::METHOD_POST)
    ->setData(
        [
            'username' => 'kadalkesit',
            'password' => 'superkesit',
        ]
    )
    ->addQuery('

$response->isError(); // true|false (status >= 400)
$response->isClientError(); // true|false (status 400 -> 499)
$response->isServerError(); // true|false (status 500 -> 520)

$response->getContentType(); // application/json, text/html, text/plain, application/xml
$response->getContent(); // get result body (string)
$response->getHeaders(); // response header