PHP code example of pbmengine / php-restclient

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

    

pbmengine / php-restclient example snippets

bash
composer 
 php

use Pbmengine\Restclient\HttpClient;

$client = new HttpClient; 

// or

$client = new HttpClient(\GuzzleHttp\Client, 'https://example.com/v1', ['timeout' => 30]); 

$response = $client->get('users');
$response = $client->jsonPayload(['id' : 1])->post('users');


$client->baseUrl('https://example.com/v1');

$client->options(['http_errors' => false, 'timeout' => 30]);
$client->option('http_errors', false);

$client->authorizationBearer('token');
$client->authorizationHttp('username', 'password');
$client->authorizationDigest('username', 'password');

$client->headers(['API-KEY' => 30]);
$client->header('API-KEY', 30);

$client->queryParams(['embed' => 'resource']);
$client->queryParam('embed', 'resource');

$client->jsonPayload([]);
$client->multipartPayload([]);
$client->formParamsPayload([]);

$client->getHeaders();
$client->getQueryParams();
$client->getBody();
$client->getRequestUrl('endpoint');

$response = $client->get('users');
$response = $client->post('users');
$response = $client->put('users/12');
$response = $client->delete('users/12');
$response = $client->patch('users/12');
$response = $client->head('users/12');



$response->statusCode(); // 200
$response->headers(); // []
$response->raw(); // ResponseInterface
$response->raw()->getHeaders(); // []
$response->isValid(); // true
$response->isServerError(); // false
$response->isClientError(); // false
$response->content(); // StdClass
$response->contentAsArray(); // Array
$response->contentAsJson(); // Json String
$response->contentAsCollection(); // Illuminate Collection



$client = (new HttpClient)
    ->baseUrl('https://example.com')
    ->authorizationBearer('your token');

// get all users    
$response = $client
    ->queryParam('embed', 'client');
    ->get('users'); 

// update user
$response = $client
    ->jsonPayload(['name' => 'John'])
    ->put('users/' . $response->content()->data->id);

// set new Bearer
$client->authorizationBearer('another token');

// delete user
$response = $client->delete('users/id');