Skip to content

A http client wrapper/PSR-7/PSR-17/PSR-18 implementation for PHP 7.4+.

License

Notifications You must be signed in to change notification settings

chillerlan/php-httpinterface

Repository files navigation

chillerlan/php-httpinterface

A PSR-7/PSR-17/PSR-18 implementation for PHP 7.2+.

version license Travis Coverage Scrunitizer Packagist downloads PayPal donate

Documentation

Requirements

  • PHP 7.2+
    • the cURL extension if you plan to use the CurlClient class

Installation

requires composer

composer.json (note: replace dev-master with a version boundary)

{
	"require": {
		"php": "^7.2",
		"chillerlan/php-httpinterface": "dev-master"
	}
}

Manual installation

Download the desired version of the package from master or release and extract the contents to your project folder. After that:

  • run composer install to install the required dependencies and generate /vendor/autoload.php.
  • if you use a custom autoloader, point the namespace chillerlan\HTTP to the folder src of the package

Profit!

Usage

A HTTPClientInterface is usually invoked with a HTTPOptions object as the first (optional) parameter, and - depending on the client - followed by one or more optional PSR-17 message factories.

$options = new HTTPOptions([
	'ca_info'    => '/path/to/cacert.pem',
	'user_agent' => 'my cool user agent 1.0',
]);

$http = new CurlClient($options, $myResponseFactory);

You can now fire a request via the implemented PSR-18 method ClientInterface::sendRequest(), using an existing PSR-7 RequestInterface...

use chillerlan\HTTP\Psr7\Request;

$request = new Request('GET', 'https://www.example.com?foo=bar');

$http->sendRequest($request);

...or you can use the HTTPClientInterface::request() method, which creates a new request using the provided (if any) factories. The HTTPClientInterface also provides constants for the HTTP methods via the RequestMethodInterface.

$http->request('https://www.example.com', $http::METHOD_GET, ['foo' => 'bar']);

Both methods will return a PSR-7 ResponseInterface.

PSR-7 Message helpers

These static methods can be found in the chillerlan\HTTP\Psr7 namespace:

  • normalize_request_headers(array $headers)
  • r_rawurlencode($data) - recursive rawurlencode, accepts a string or an array as input
  • build_http_query(array $params, bool $urlencode = null, string $delimiter = null, string $enclosure = null) - see abraham/twitteroauth
  • clean_query_params(iterable $params, int $bool_cast = null, bool $remove_empty = null) - clean an array of parameters for URL queries (or JSON output etc.) using the following cast formats:
    • BOOLEANS_AS_BOOL - bool types will be left untouched (default)
    • BOOLEANS_AS_INT - cast to integer 1 and 0
    • BOOLEANS_AS_STRING - a string value "true" and "false"
    • BOOLEANS_AS_INT_STRING - integer values, but as string, "1" and "0"
  • merge_query(string $uri, array $query) - merges an array of parameters into an URL query string
  • normalize_files(array $files)
  • create_uploaded_file_from_spec(array $value)
  • normalize_nested_file_spec(array $files = [])
  • get_json(ResponseInterface $response, bool $assoc = null)
  • get_xml(ResponseInterface $response)
  • message_to_string(MessageInterface $message) - returns the string representation of a MessageInterface
  • decompress_content(MessageInterface $message) - decompresses the message content according to the Content-Encoding header and returns the decompressed data

PSR-17 Factory helpers

These static methods can be found in the chillerlan\HTTP\Psr17 namespace:

  • create_server_request_from_globals() - creates a PSR-7 ServerRequestInterface object that is populated with the GPCS superglobals.
  • create_uri_from_globals() - creates a PSR-7 UriInterface object that is populated with values from $_SERVER.
  • create_stream(string $content = '') - creates a PSR-7 StreamInterface object from a string.
  • create_stream_from_input($in = null) - creates a PSR-7 StreamInterface object from guessed input (string/scalar, file path, resource, object)