ddrv/http-client

simple HTTP client without cURL dependency

v2.0.3 2020-12-14 12:47 UTC

This package is auto-updated.

Last update: 2024-12-14 21:18:44 UTC


README

Latest Stable Version Total Downloads License PHP

ddrv/http-client

Simple HTTP client without cURL dependency. Required allow_url_fopen option in php.ini file.

; php.ini
allow_url_fopen = On

Install

Install this package, your favorite psr-7 implementation and your favorite psr-17 implementation.

composer require ddrv/http-client:^2.0

Using

<?php

use Ddrv\Http\Client\Client;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;

/** 
 * @var ResponseFactoryInterface $responseFactory
 * @var int $timeout = 60
 * @var int $redirects = 0
 */
$http = new Client($responseFactory, $timeout, $redirects);

/** @var RequestInterface $request */
$response = $http->sendRequest($request);

$code = $response->getStatusCode();
$phrase = $response->getReasonPhrase();
$headers = $response->getHeaders();
$someHeader = $response->getHeader('Content-Type');

$body = $response->getBody()->__toString();

Configuration

<?php

use Ddrv\Http\Client\Client;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\UriInterface;

/**
 * @var ResponseFactoryInterface $responseFactory
 * @var UriInterface $proxy
 */

$http = new Client($responseFactory);

$http->setFollowRedirects(0); // Set 0 follow redirects (disable). 
$http->setTimeOut(10); // Set connection timeout 10 seconds

$http->setProxy($proxy); // Set proxy
$http->setProxy(); // Unset proxy

SSL Authorization

<?php

use Ddrv\Http\Client\Client;
use Psr\Http\Message\ResponseFactoryInterface;

/**
 * @var ResponseFactoryInterface $responseFactory
 */

$http = new Client($responseFactory);

$http->setSslAuth('host', 'ssl.crt', 'ssl.key'); // without password
$http->setSslAuth('host', 'ssl.crt', 'ssl.key', 'p@s$w0rd'); // with password
$http->unsetSslAuth('host'); // disable