PHP code example of bradleyhodges / api-manager

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

    

bradleyhodges / api-manager example snippets


use APIManager\APIManager;

// Initialize the APIManager
$apiManager = new APIManager();

use APIManager\APIManager;

// Initialize the APIManager
$apiManager = new APIManager();

// Initialize the HTTP client
$http = $apiManager->useHTTP();

// Send a GET request
$response = $http->get('https://google.com');

// Get the response body
$body = $response->getBody()->getContents();

// Get the response code and reason phrase
$responseCode = $response->getStatusCode(); // 200
$responseReason = $response->getReasonPhrase(); // OK

// More methods and examples at https://docs.guzzlephp.org/en/stable/quickstart.html#using-responses
// Just replace `$client` with `$http` to use them

$http = (new APIManager())->useHTTP();

use APIManager\HTTP;

$http = new HTTP();

$http->useRetry(true)
     ->useDnsCache(true)
     ->useHttpVersionFallback(true);

// Send a POST request with a Bearer token
$http->setBearerToken('your-token-here');
$response = $http->post('/submit', ['json' => ['key' => 'value']]);

use APIManager\HTTP;

$http = new HTTP([
    // Base URI is used with relative requests
    'base_uri' => 'http://httpbin.org',
    // You can set any number of default request options.
    'timeout'  => 2.0,
]);

use APIManager\HTTP;

// Initialize the HTTP client
$http = new HTTP();

// Access the Guzzle client instance
$client = $http->getClient();

// Send an asynchronous request using Guzzle's magic methods:
$promise = $client->getAsync('http://httpbin.org/get');

$apiManager->useCORS([
  'allowedOrigins' => ['https://example.com'],
  'allowCredentials' => true,
  'allowedMethods' => ['GET', 'POST', 'OPTIONS'],
  'allowedHeaders' => ['Content-Type', 'Authorization'],
  'exposedHeaders' => ['X-Custom-Header'],
  'maxAge' => 3600,
]);

$apiManager->useCORS([
  'allowedOrigins' => ['https://example.com'],
]);

$responseManager = $apiManager->responseManager();

// Add a message to the response
$responseManager->addMessage('Hello, world!');

// Add an error to the response
$responseManager->addError([
  'status' => "422",
  'source' => ['pointer' => '/data/attributes/first-name'],
  'title' => 'Invalid Attribute',
  'detail' => 'First name must contain at least three characters.',
]);

// Respond with data
$responseManager->respond(true, ['some key' => 'some value']); // Successful response

// Respond with an error
$responseManager->respond(false, [], 400); // Error response with optional status code

$apiManager->useRateLimiter();

$apiManager->useRateLimiter([
  'id' => 'api_limit',
  'policy' => 'sliding_window',
  'limit' => 100,
  'interval' => '1 minute',
]);

$apiManager->useCsrfManager();

// Adding a custom header
$apiManager->addHeader('X-Custom-Header', 'CustomValue');

// Removing or preventing a header
$apiManager->removeHeader('X-Frame-Options');

echo $apiManager->sanitizeInput('some input');