PHP code example of wp-oop / http-client

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

    

wp-oop / http-client example snippets


use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use WpOop\HttpClient\Client;

/** @var RequestFactoryInterface $requestFactory */
/** @var ResponseFactoryInterface $responseFactory */

// Set up the client with WP request options
// https://developer.wordpress.org/reference/classes/wp_http/request/
$client = new Client(
    // Default args to WP_Http::request()
    [
        'redirection' => 2,
        'timeout' => 60,
    ],
    $responseFactory,
    // Path to proxy file dir to enable response streaming.
    // If null, will buffer in memory instead.
    get_temp_dir() 
);

// Create a PSR-7 request in any way you want, for example via a PSR-17 factory
$request = $requestFactory->createRequest('GET', 'http://somesite.com/api');

try {
    // Send a request as usual, consuming a familiar PSR-18 interface
    $response = $client->sendRequest($request);
} catch (ClientExceptionInterface $e) {
    // Handle PSR-18 exceptions
}