PHP code example of arturdoruch / http

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

    

arturdoruch / http example snippets


use ArturDoruch\Http\Client;

$client = new Client();
// Send GET request.
$response = $client->get('http://httpbin.org/get');

// Get response status code.
$statusCode = $response->getStatusCode();

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

// Display response raw headers and body.
echo $response;

// Display response headers.
foreach ($response->getHeaders() as $name => $value) {
    echo sprintf("%s: %s\n", $name, $value);
}

use ArturDoruch\Http\Cookie\CookieFile;
use ArturDoruch\Http\Client;

// Set the cURL options, which will be used for send every HTTP request.
$curlOptions = [
    'followlocation' => false,
    'timeout' => 120
];

// Enabled or disabled throwing RequestException, when request is complete and response status code is 4xx, 5xx or 0.
$throwExceptions = true;

// Set file where all HTTP session cookies should be stored.
$cookieFile = new CookieFile('path/to/cookies.txt');

$client = new Client($curlOptions, $throwExceptions, $cookieFile);

$response = $client->get('http://httpbin.org/get');
$response = $client->post('http://httpbin.org/post');
$response = $client->patch('http://httpbin.org/patch');
$response = $client->put('http://httpbin.org/put');
$response = $client->delete('http://httpbin.org/delete');

use ArturDoruch\Http\Request;

$request = new Request('DELETE', 'http://httpbin.org/delete');
$response = $client->request($request);

$requests = [
    // The list of ArturDoruch\Http\Request objects or URLs to send. 
];
$responses = $client->multiRequest($requests);

foreach ($responses as $response) {
    var_dump($response->getBody());
}

$formData = [
    'name' => 'value',
    'choices' => [1, 2, 3]
];

$response = $client->post('http://httpbin.org/post', $formData);

$request = new Request('POST', 'http://httpbin.org/post', $formData);
$response = $client->request($request);

    $client->get('/get', [], [
        'cookie' => 'NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure'
    ]);
    

    $client->get('/get', [], [
        'headers' => [
            'User-Agent' => 'testing/1.0',
            'Accept' => 'application/json',
            'X-Foo' => ['Bar', 'Baz']
        ]
    ]);
    

    // Send body as plain text taken from a resource.
    $resource = fopen('http://httpbin.org', 'r');
    $client->post('/post', [], ['body' => $resource]);
    
    // Send plain text.
    $client->post('/post', [], ['body' => 'Raw data']);
    

    $client->post('/post', [], [
        'json' => [
            'foo' => 'bar',
            'key' => 'value'
        ]
    ]);
    

    use ArturDoruch\Http\Message\FormFile;
    
    $client->post('/post', [], [
        'multipart' => [
            'name' => 'value',
            'categories' => [
                'animals' => ['dog', 'cat'],
            ],
            'file' => new FormFile('/path/file.txt.', 'optional-custom-filename.txt')
        ]        
    ]);
    

use App\EventListener\HttpRequestListener;
use ArturDoruch\Http\Event\BeforeEvent;
use ArturDoruch\Http\Event\CompleteEvent;
use ArturDoruch\Http\Event\RequestEvents;

// Add listener to request.before event as anonymous function.
$client->addListener(RequestEvents::BEFORE, function (BeforeEvent $event) {
    $request = $event->getRequest();
});

// Add listener to request.before event as method class.
$client->addListener(RequestEvents::BEFORE, [new HttpRequestListener(), 'onBefore']);
    
// Add listener to request.complete event as method class.
$client->addListener(RequestEvents::COMPLETE, [new HttpRequestListener(), 'onComplete']);

namespace App\EventListener;

use ArturDoruch\Http\Event\BeforeEvent;
use ArturDoruch\Http\Event\CompleteEvent;

class HttpRequestListener
{    
    /**
     * @param BeforeEvent $event
     */
    public function onBefore(BeforeEvent $event)
    {
        $request = $event->getRequest();
        // Do some actions before HTTP request is sending.
    }

    /**
     * @param CompleteEvent $event
     */
    public function onComplete(CompleteEvent $event)
    {
        $response = $event->getResponse();
        // Do some actions when HTTP request is complete.
    }
}

$responseArray = $response->toArray();

$responseJson = $response->toJson();
// Use JSON_PRETTY_PRINT option to format output json
$responseJson = $response->toJson(true);

// Expose only the "statusCode" and "body" properties.
$response->expose([
    'statusCode',
    'body',
]);    
// The array will contain only "statusCode" and "body" keys.    
$responseArray = $response->toArray();
    
// Expose all properties.
$response->exposeAll();    
// The array will contain all of available properties.  
$responseArray = $response->toArray();    

    $client = new Client([CURLINFO_HEADER_OUT => true]);
        
    $response = $client->get('http://httpbin.org/get');
    $curlInfo = $response->getCurlInfo()
    
    $requestHeaders = $curlInfo['request_header'];