extensions:
httpClient: Fapi\HttpClient\Bidges\NetteDI\HttpClientExtension
The default configuration options are
httpClient:
type: guzzle
logging: false
bar: false
Option type
specifies the type of HTTP client to be used. Supported types are guzzle
and curl
.
Option logging
enables logging of all HTTP requests, HTTP responses and HTTP client errors.
Option bar
enables Tracy bar of all HTTP requests, HTTP responses and HTTP client errors.
You can use only logging
or bar
option. Both are not allowed for now.
Logging supports Psr-3 Psr\Log\LoggerInterface
. If you are using Tracy\ILogger
you can use bridge Fapi\HttpClient\Bridges\Tracy\TracyToPsrLogger
to convert tracy to psr logger.
Log formatting:
You can create your own logging formatter. By implementing ILoggingFormatter
and then passing your formatter into LoggerHttpClient
as 3rd argument.
By default BaseLoggingFormatter
is used.
$loggingClient = new LoggingHttpClient(
$httpClient,
new TracyToPsrLogger(Debugger::getLogger()),
new MyCustomLoggingFormatter()
);
Downloading Google homepage is as easy as
$httpRequest = HttpRequest::from('https://www.google.com/');
$httpResponse = $httpClient->sendRequest($httpRequest);
The HTTP response object has methods for retrieving status code, response headers and response body.
Option form_params
can be used for making POST request with Content-Type: application/x-www-form-urlencoded
.
$httpRequest = HttpRequest::from('https://www.example.com/login', HttpMethod::POST, [
'form_params' => [
'username' => 'admin',
'password' => 'xxx',
],
]);
Option headers
can be used for specifying HTTP request headers.
$httpRequest = HttpRequest::from('https://www.google.com/', HttpMethod::GET, [
'headers' => [
'User-Agent' => 'Bot/1.0',
],
]);
Option auth
can be used for specifying credentials for HTTP basic authentication.
$httpRequest = HttpRequest::from('https://www.example.com/private', HttpMethod::GET, [
'auth' => ['admin', 'xxx'],
]);
Option body
can be used for specifying request body.
$httpRequest = HttpRequest::from('https://www.example.com/api', HttpMethod::POST, [
'body' => 'Request body',
]);
Option json
can be used for specifying request body data, which should be serialized to JSON before sending.
$httpRequest = HttpRequest::from('https://www.example.com/api', HttpMethod::POST, [
'json' => [
'foo' => 'bar',
],
]);
Option cookies
can be used for specifying a cookie jar. This option is only supported by the Guzzle HTTP client.
Option cookies
is no more supported.
Option connect_timeout
can be used for specifing connection time limit.
$httpRequest = HttpRequest::from('https://www.example.com/', HttpMethod::GET, [
'connect_timeout' => 3,
]);
Option timeout
can be used for specifing response time limit.
$httpRequest = HttpRequest::from('https://www.example.com/', HttpMethod::GET, [
'timeout' => 3,
]);
If you want to follow redirects, use RedirectHelper after sending HTTP request.
$httpResponse = $httpClient->sendHttpRequest($httpRequest);
$httpResponse = RedirectHelper::followRedirects($httpClient, $httpResponse, $originHttpRequest);
Let's assume you want to test class Foo
which uses an HTTP client.
namespace SampleProjectTests;
use Fapi\HttpClient\CapturingHttpClient;
use Fapi\HttpClient\GuzzleHttpClient;
use SampleProject\Foo;
use SampleProjectTests\MockHttpClients\FooMockHttpClient;
use Tester\Assert;
use Tester\TestCase;
class FooTest extends TestCase
{
/**
* @var CapturingHttpClient|FooMockHttpClient
*/
private $httpClient;
public function __construct()
{
$this->httpClient = new CapturingHttpClient(
new GuzzleHttpClient(),
__DIR__ . '/MockHttpClients/FooMockHttpClient.php',
'SampleProjectTests\MockHttpClients\FooMockHttpClient'
);
}
public function __destruct()
{
$this->httpClient->close();
}
public function testDownloadData()
{
$foo = new Foo($this->httpClient);
Assert::same(42, $foo->downloadData());
}
}
After the test succeeds, stored requests and responses are written to mock file. Next time you run tests MockHttpClient is used.
You can always update the mock client by deleting generated file.
The tests will then be deterministic. You will be able to run them even without an Intenet connection or when the remote service is down. Furthermore, they will run much faster.
You can use class RestClient
for accessing JSON REST APIs protected by HTTP basic authentication. It provides methods for creating, getting, updating and deleting resources.