PHP code example of demi / php-api-client

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

    

demi / php-api-client example snippets




$client = new \demi\api\Client([
    'baseUri' => 'https://google.com/api/',
    'timeout' => 30,
    'defaultHeaders' => [],
    'defaultQueryParams' => [],
]);

// Simple example: get index page content
$content = $client->get('http://example.com/index.php')->send()->body();

// Make request to baseUri + 'users'
$request = $client->post('users')
    ->setQueryParam('id', 123) // Single param
    ->setQueryParam(['name' => 'Jack', 'company' => 'Google']) // Params array
    ->setPostParam('password', '12345') // Single POST param
    ->setPostParam(['email' => 'example@com', 'location' => 'London']) // POST params array
    ->setHeaderParam('Connection', 'Keep-Alive') // Header value
    ->setHeaderParam(['Accept' => 'image/gif', 'Some-Custom' => 'value']); // Headers array

// Resets
$request->queryParams = []; // Reset query params
$request->formParams = []; // Reset post params
$request->headerParams = []; // Reser headers

// Submit request and get Response object
$response = $request->send();



// Static calls
$response = \demi\api\ApiRequest::get('http://example.com/index.php');
$response = \demi\api\ApiRequest::post('http://example.com/index.php');
$response = \demi\api\ApiRequest::put('http://example.com/index.php');
$response = \demi\api\ApiRequest::delete('http://example.com/index.php');

$statusCode = $response->statusCode(); // Response code: 200, 201, 204, etc...
$bodyText = $response->body(); // Content
$bodyJson = $response->json(); // Json decoded content
$headerParams = $response->headers(); // Headers array
$headerValue = $response->headerValue('Encoding', 'Default value'); // Some header value
code
composer