PHP code example of liopoos / booze

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

    

liopoos / booze example snippets


use Liopoos\Booze\Client;

class ApiClient extends Client
{
    public function __construct(array $guzzleOptions = [])
    {
        // Configure Guzzle options
        $guzzleOptions = array_merge([
            'base_uri' => 'https://api.example.com',
            'timeout' => 30,
        ], $guzzleOptions);

        parent::__construct($guzzleOptions);
    }
}

$client = new ApiClient();
$response = $client->get('/users');

// Simple GET request
$response = $client->get('https://httpbin.org/get');

// GET with query parameters
$response = $client->get('https://httpbin.org/get', [
    'page' => 1,
    'limit' => 10
]);

// GET with custom headers
$response = $client->get('https://httpbin.org/get', [], [
    'Authorization' => 'Bearer token123'
]);

// POST with form data
$response = $client->post('https://httpbin.org/post', [
    'name' => 'John Doe',
    'email' => '[email protected]'
]);

// POST with JSON
$response = $client->postJson('https://httpbin.org/post', [
    'name' => 'John Doe',
    'email' => '[email protected]'
]);

// POST with multipart (file uploads)
$response = $client->postMultiPart('https://httpbin.org/post', [
    [
        'name' => 'file',
        'contents' => fopen('/path/to/file.jpg', 'r'),
        'filename' => 'file.jpg'
    ]
]);

// PUT with form data
$response = $client->put('https://httpbin.org/put', [
    'name' => 'Jane Doe'
]);

// PUT with JSON
$response = $client->putJson('https://httpbin.org/put', [
    'name' => 'Jane Doe'
]);

// PUT with multipart
$response = $client->putMultiPart('https://httpbin.org/put', [
    [
        'name' => 'data',
        'contents' => 'value'
    ]
]);

// PATCH with JSON (default)
$response = $client->patch('https://httpbin.org/patch', [
    'name' => 'Updated Name'
]);

// Simple DELETE
$response = $client->delete('https://httpbin.org/delete');

// DELETE with JSON body
$response = $client->deleteJson('https://httpbin.org/delete', [
    'reason' => 'No longer needed'
]);

$client->withHeaders([
    'Authorization' => 'Bearer your-token',
    'Accept' => 'application/json',
    'User-Agent' => 'MyApp/1.0'
]);

// All subsequent requests will 

// JSON responses are automatically decoded to arrays
$response = $client->get('https://api.example.com/users');
// $response is an array

// XML responses are converted to arrays
$response = $client->get('https://api.example.com/data.xml');
// $response is an array

// Plain text responses are returned as strings
$response = $client->get('https://example.com/text');
// $response is a string

use Liopoos\Booze\Exception\UnauthorizedHttpException;
use Liopoos\Booze\Exception\NotFoundHttpException;
use Liopoos\Booze\Exception\AccessDeniedHttpException;
use Liopoos\Booze\Exception\ApiException;

try {
    $response = $client->get('https://api.example.com/protected');
} catch (UnauthorizedHttpException $e) {
    // Handle 401 Unauthorized
    echo "Authentication failed: " . $e->getMessage();
} catch (AccessDeniedHttpException $e) {
    // Handle 403 Forbidden
    echo "Access denied: " . $e->getMessage();
} catch (NotFoundHttpException $e) {
    // Handle 404 Not Found
    echo "Resource not found: " . $e->getMessage();
} catch (ApiException $e) {
    // Handle all other HTTP errors (4xx, 5xx)
    echo "API error: " . $e->getMessage();
}

use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;

class MyApiClient extends Client
{
    public function __construct()
    {
        $handler = HandlerStack::create();
        
        // Add custom middleware
        $handler->push(Middleware::log(
            $logger,
            new MessageFormatter('{method} {uri} HTTP/{version} {code}')
        ));
        
        parent::__construct([
            'handler' => $handler,
            'base_uri' => 'https://api.example.com',
            'timeout' => 30,
            'verify' => true,
            'http_errors' => false,
        ]);
    }
}

// Get the underlying Guzzle client
$guzzleClient = $client->getHttpClient();

// Get the last HTTP response object
$client->get('https://httpbin.org/get');
$httpResponse = $client->getHttpResponse();

// Access response details
$statusCode = $httpResponse->getStatusCode();
$headers = $httpResponse->getHeaders();
$body = $httpResponse->getBody();