larium/http-client

An http client implemention compatible with psr-7 http message.

1.2.1 2017-07-08 10:13 UTC

README

An http client wrapping curl php extension, compatible with PSR-7 Http Message interfaces.

Installation

You can install this library using Composer

Composer from command line

In root directory of your project run through a console:

$ composer require "larium/http-client":"~1.0"

Composer from composer.json

Include require line in your composer.json file

{
	"require": {
    	"larium/http-client": "~1.0"
    }
}

and run from console in the root directory of your project:

$ composer update

Set up

After installation you must require autoload file from composer in to your boot php script.

<?php
require_once 'vendor/autoload.php';

Basic usage

Using Diactoros

You can use any Request class that implements PSR-7 HTTP Message interfaces, to create the Request instance. In this example, Zend Diactoros will be used.

<?php

use Zend\Diactoros\Uri;
use Larium\Http\Client;
use Zend\Diactoros\Request;
use Larium\Http\Exception\ClientException;

$uri = (new Uri())->withScheme('http')->withHost('www.example.com');
$request = (new Request())->withUri($uri);
$client = new Client();
try {
	$response = $client->send($request);
    # Response is a Psr\Http\Message\ResponseInterface instance implementation.
} catch (ClientException $e) {
	//Resolve exception from client.
}

Using message factory

<?php

use Larium\Http\Client;
use Larium\Http\Exception\ClientException;
use Http\Discovery\MessageFactoryDiscovery;

$messageFactory = MessageFactoryDiscovery::find();

$request = $messageFactory->createRequest('get', 'http://example.com');

$client = new Client();
try {
	$response = $client->send($request);
    # Response is a Psr\Http\Message\ResponseInterface instance implementation.
} catch (ClientException $e) {
	//Resolve exception from client.
}