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);
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());
}
// 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']);
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();