To send a request, you can use the API defined by the Ivory\HttpAdapter\HttpAdapterInterface
. All these methods
throw an Ivory\HttpAdapter\HttpAdapterException
if an error occurred (I would recommend you to always use a try/catch
block everywhere) and return an Ivory\HttpAdapter\Message\ResponseInterface
.
Additionally, the url can be a string or an Psr\Http\Message\UriInterface
. The headers parameter can be an
associative array describing an header key/value pair or an indexed array already formatted. The datas can be an
associative array or a string already formatted according to the content-type you want to use. Finally, the files are
an associative array describing key/path pair.
$response = $httpAdapter->get($url, $headers);
$response = $httpAdapter->head($url, $headers);
$response = $httpAdapter->trace($url, $headers);
$response = $httpAdapter->post($url, $headers, $datas, $files);
$response = $httpAdapter->put($url, $headers, $datas, $files);
$response = $httpAdapter->patch($url, $headers, $datas, $files);
$response = $httpAdapter->delete($url, $headers, $datas, $files);
$response = $httpAdapter->options($url, $headers, $datas, $files);
$response = $httpAdapter->send($url, $method, $headers, $datas, $files);
All methods are described by the Ivory\HttpAdapter\Message\RequestInterface::METHOD_*
constants.
use Ivory\HttpAdapter\Message\InternalRequest;
use Ivory\HttpAdapter\Message\Request;
$response = $httpAdapter->sendRequest(new Request($url, $method));
// or
$response = $httpAdapter->sendRequest(new InternalRequest($url, $method));
If you want to learn more about the requests, your can read this doc.
The main purpose of this method is performance! Instead of sending requests serially, the library will send them in parallel if the sub adapter is able to do it otherwise, it will fallback on a serial implementation.
use Ivory\HttpAdapter\Message\InternalRequest;
use Ivory\HttpAdapter\Message\Request;
use Ivory\HttpAdapter\MultiHttpAdapterException;
$requests = array(
// An url (GET 1.1)
'http://egeloen.fr',
// An array representing the parameters of the `MessageFactoryInterface::createInternalRequest`
array('http://egeloen.fr', 'GET', 1.1, array('Content-Type' => 'json', '{"foo":"bar"}')),
// A PSR-7 request
new Request('http://egeloen.fr', 'GET'),
// An internal request
new InternalRequest('http://egeloen.fr', 'GET'),
);
try {
$responses = $httpAdapter->sendRequests($requests);
} catch (MultiHttpAdapterException $e) {
$responses = $e->getResponses();
$exceptions = $e->getExceptions();
}