1. Go to this page and download the library: Download dormilich/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/ */
dormilich / http-client example snippets
use Dormilich\HttpClient\Client;
// replace this with the actual implementations
$httpClient = new HttpClient(); // PSR-18
$requestFactory = new RequestFactory(); // PSR-17
$streamFactory = new StreamFactory(); // PSR-17
$client = new Client($httpClient, $requestFactory, $streamFactory);
use Dormilich\HttpClient\Client;
$client = new Client($httpClient, $requestFactory, $streamFactory);
$client->getHeaders()->add('User-Agent', 'curl/7.64.1');
// all requests will now contain the `User-Agent` header
use Dormilich\HttpClient\Client;
$client = new Client($httpClient, $requestFactory, $streamFactory);
$request = new Request('GET', 'https://example.com'); // PSR-7
// $result will contain the response body content
$result = $client->request($request);
use Dormilich\HttpClient\Client;
use Dormilich\HttpClient\Transformer\JsonEncoder;
$client = new Client($httpClient, $requestFactory, $streamFactory);
$client->addTransformer(new JsonEncoder());
$client->get('https://example.com/item');
$client->post('https://example.com/item', $data);
$client->put('https://example.com/item', $data);
$client->patch('https://example.com/item', $data);
$client->delete('https://example.com/item');
use Dormilich\HttpClient\Client;
use Dormilich\HttpClient\Transformer\UrlEncoder;
$client = new Client($httpClient, $requestFactory, $streamFactory);
$client->addTransformer(new UrlEncoder());
$search['foo'] = 'bar';
$header['authorization'] = 'Bearer 279ca9e0-ce59-48b2-8b6d-c0a6822195a1';
$result = $client->fetch('get', 'https://example.com/item', $search, $header);
use Dormilich\HttpClient\Client;
use Dormilich\HttpClient\Transformer\DomTransformer;
use Dormilich\HttpClient\Transformer\TextTransformer;
use Dormilich\HttpClient\Transformer\XmlTransformer;
$client = new Client($httpClient, $requestFactory, $streamFactory);
# converts DOMDocument
$client->addTransformer(new DomTransformer());
# converts data that can be cast to string
$client->addTransformer(new TextTransformer());
# converts SimpleXML objects
$client->addTransformer(new XmlTransformer());
$result = $client->post('https://example.com/item', $data);
use Dormilich\HttpClient\Client;
use Dormilich\HttpClient\Transformer\JsonEncoder;
use Dormilich\HttpClient\Transformer\UrlEncoder;
$client = new Client($httpClient, $requestFactory, $streamFactory);
# converts JsonSerializable & plain objects
$client->addTransformer(new JsonEncoder());
# converts arrays
$client->addTransformer(new UrlEncoder());
$result = $client->post('https://example.com/item', $data);
use Dormilich\HttpClient\Client;
use Dormilich\HttpClient\Transformer\JsonDecoder;
use Dormilich\HttpClient\Transformer\UrlDecoder;
$client = new Client($httpClient, $requestFactory, $streamFactory);
$client->addTransformer(new JsonDecoder(JSON_OBJECT_AS_ARRAY));
$client->addTransformer(new UrlDecoder());
$result = $client->post('https://example.com/item', $data);
use Dormilich\HttpClient\Client;
use Dormilich\HttpClient\Decoder\Decoder;
use Dormilich\HttpClient\Transformer\JsonDecoder;
use Dormilich\HttpClient\Utility\StatusMatcher;
$decoder = new Decoder(new JsonDecoder());
$decoder->setStatusMatcher(StatusMatcher::clientError());
$client = new Client($httpClient, $requestFactory, $streamFactory);
// only decodes HTTP 4xx errors in JSON format
$client->addDecoder($decoder);
use Dormilich\HttpClient\Transformer\JsonEncoder;
$default = new JsonEncoder();
$slashes = new JsonEncoder(JSON_UNESCAPED_SLASHES);
$data = 'text/plain';
$result = $default->encode($data); // "text\/plain"
$result = $slashes->encode($data); // "text/plain"
use Dormilich\HttpClient\Transformer\JsonDecoder;
$object = new JsonDecoder();
$array = new JsonDecoder(JSON_OBJECT_AS_ARRAY);
$json = '{"foo":"bar"}';
$result = $object->decode($json); // $result->foo = 'bar';
$result = $array->decode($json); // $result['foo'] = 'bar';
use Dormilich\HttpClient\Client;
use Dormilich\HttpClient\Encoder\Header\ContentLength;
$client = new Client($httpClient, $requestFactory, $streamFactory);
// this will add the `Content-Length` header, if appropriate
$client->addEncoder(new ContentLength());
use Psr\Log\LoggerInterface;
use Dormilich\HttpClient\Client;
use Dormilich\HttpClient\Decoder\ErrorDecoder;
use Dormilich\HttpClient\Exception\RequestException;
use Dormilich\HttpClient\Transformer\JsonDecoder;
try {
$client = new Client($httpClient, $requestFactory, $streamFactory);
// converts a failed request into an exception
// using the request body as exception message
$client->addDecoder(new ErrorDecoder());
$client->addTransformer(new JsonDecoder());
$result = $client->get('https://example.com/toc')
} catch (RequestException $e) {
$context['request'] = $e->getRequest();
$context['response'] = $e->getResponse();
$logger->error($e->getMessage(), $context);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.