In order to make an http request, you need to create an adapter. An adapter is designed around the
Ivory\HttpAdapter\HttpAdapterInterface
and represents the central point of the library.
use Buzz\Browser;
use Ivory\HttpAdapter\BuzzHttpAdapter;
$httpAdapter = new BuzzHttpAdapter();
// or
$httpAdapter = new BuzzHttpAdapter(new Browser());
use Ivory\HttpAdapter\CakeHttpAdapter;
$httpAdapter = new CakeHttpAdapter();
// or
$httpAdapter = new CakeHttpAdapter(new \HttpSocket());
use Ivory\HttpAdapter\CurlHttpAdapter;
$httpAdapter = new CurlHttpAdapter();
use Ivory\HttpAdapter\FileGetContentsHttpAdapter;
$httpAdapter = new FileGetContentsHttpAdapter();
use Ivory\HttpAdapter\FopenHttpAdapter;
$httpAdapter = new FopenHttpAdapter();
use Guzzle\Http\Client;
use Ivory\HttpAdapter\Guzzle3HttpAdapter;
$httpAdapter = new Guzzle3HttpAdapter();
// or
$httpAdapter = new Guzzle3HttpAdapter(new Client());
use GuzzleHttp\Client;
use Ivory\HttpAdapter\Guzzle4HttpAdapter;
$httpAdapter = new Guzzle4HttpAdapter();
// or
$httpAdapter = new Guzzle4HttpAdapter(new Client());
use GuzzleHttp\Client;
use Ivory\HttpAdapter\Guzzle5HttpAdapter;
$httpAdapter = new Guzzle5HttpAdapter();
// or
$httpAdapter = new Guzzle5HttpAdapter(new Client());
use GuzzleHttp\Client;
use Ivory\HttpAdapter\Guzzle6HttpAdapter;
$httpAdapter = new Guzzle6HttpAdapter();
// or
$httpAdapter = new Guzzle6HttpAdapter(new Client());
use Ivory\HttpAdapter\HttpfulHttpAdapter;
$httpAdapter = new HttpfulHttpAdapter();
use Ivory\HttpAdapter\MockHttpAdapter;
use Ivory\HttpAdapter\Message\RequestInterface;
$mockHttpAdapter = new MockHttpAdapter();
$expectedResponse = $mockHttpAdapter->getConfiguration()->getMessageFactory()->createResponse(
200,
RequestInterface::PROTOCOL_VERSION_1_1,
['Content-Type: application/json'],
'{"hello":"world"}'
);
$mockHttpAdapter->appendResponse($expectedResponse);
$response = $mockHttpAdapter->send('http://www.google.com');
// $response === $expectedResponse
use Ivory\HttpAdapter\PeclHttpAdapter;
$peclHttpAdapter = new PeclHttpAdapter();
use Ivory\HttpAdapter\ReactHttpAdapter;
$reactHttpAdapter = new ReactHttpAdapter();
The React http adapter does not support all features. The limitations are:
- HTTP 1.1 not supported.
- Timeout not supported.
use Ivory\HttpAdapter\RequestsHttpAdapter;
$requestsHttpAdapter = new RequestsHttpAdapter();
// or
$requestsHttpAdapter = new RequestsHttpAdapter(new \Requests_Transport_cURL());
use Ivory\HttpAdapter\SocketHttpAdapter;
$httpAdapter = new SocketHttpAdapter();
use Ivory\HttpAdapter\Zend1HttpAdapter;
$zend1HttpAdapter = new Zend1HttpAdapter();
// or
$zend1HttpAdapter = new Zend1HttpAdapter(new \Zend_Http_Client());
use Ivory\HttpAdapter\Zend2HttpAdapter;
use Zend\Http\Client;
$zend2HttpAdapter = new Zend2HttpAdapter();
// or
$zend2HttpAdapter = new Zend2HttpAdapter(new Client());
The event dispatcher http adapter allows you to hook into the request process through the Symfony2 event dispatcher component.
use Ivory\HttpAdapter\CurlHttpAdapter;
use Ivory\HttpAdapter\EventDispatcherHttpAdapter;
use Ivory\HttpAdapter\SocketHttpAdapter;
use Symfony\Component\EventDispatcher\EventDispatcher;
$httpAdapter = new CurlHttpAdapter();
// or
$httpAdapter = new SocketHttpAdapter();
$eventDispatcher = new EventDispatcher();
$eventDispatcherHttpAdapter = new EventDispatcherHttpAdapter($httpAdapter, $eventDispatcher);
The event documentation is available here.
The stopwatch http adapter allows you to time the http adapter process (including subscribers, etc) through the Symfony2 stopwatch component.
use Ivory\HttpAdapter\CurlHttpAdapter;
use Ivory\HttpAdapter\SocketHttpAdapter;
use Ivory\HttpAdapter\StopwatchHttpAdapter;
use Symfony\Component\Stopwatch\Stopwatch;
$httpAdapter = new CurlHttpAdapter();
// or
$httpAdapter = new SocketHttpAdapter();
$stopwatch = new Stopwatch();
$stopwatchHttpAdapter = new StopwatchHttpAdapter($httpAdapter, $stopwatch);
You can either construct your http adapter through a factory. For example, in order to create a curl http adapter, you can do:
use Ivory\HttpAdapter\HttpAdapterFactory;
$httpAdapter = HttpAdapterFactory::create('curl');
The available adapters are: buzz
, cake
, curl
, file_get_contents
, fopen
, guzzle
, guzzle_http
, httpful
,
pecl_http
, react
, socket
, zend1
or zend2
.
If you want to know if an adapter is available on your system, you can use:
use Ivory\HttpAdapter\HttpAdapterFactory;
$httpAdapter = HttpAdapterFactory::capable(HttpAdapterFactory::BUZZ);
If you are not aware of the available adapters and just want to pick one, you can use:
use Ivory\HttpAdapter\HttpAdapterFactory;
$httpAdapter = HttpAdapterFactory::guess();
// or with a specific preference
$httpAdapter = HttpAdapterFactory::guess(HttpAdapterFactory::BUZZ);
// or with multiple preferences
$httpAdapter = HttpAdapterFactory::guess(array(
HttpAdapterFactory::BUZZ,
HttpAdapterFactory::HTTPFUL,
));
You can additionally register your own http adapters:
use Ivory\HttpAdapter\HttpAdapterFactory;
HttpAdapterFactory::register('my_http_adapter', 'My\Own\HttpAdapter');
$httpAdapter = HttpAdapterFactory::create('my_http_adapter');
The register
method takes a third optional parameters which represents the client used. It is used internally in
order to determine if the adapters is available. It can be either a class name, a function name or an ini option. If
you don't provide it, we consider your adapter as available.