-
Notifications
You must be signed in to change notification settings - Fork 53
HTTP clients
This library brings you two (three) HTTP client implementations. Milo\Github\Http\CurlClient and Milo\Github\Http\StreamClient. cURL client is used as default when cURL extension is loaded. If not, StreamClient is used.
Client uses cURL extension. You can pass cURL options to its constructor. Some of them are hard-coded in class implementation, check out the source code. The client is faster than StreamClient because it uses HTTP connection keep-alive.
# Without parameters
$client = new Milo\Github\Http\CurlClient;
# Set some parameters
$client = new Milo\Github\Http\CurlClient([
CURLOPT_CAINFO => '/path/to/trusted-ca.pem',
CURLOPT_CONNECTTIMEOUT => 2,
]);
This client uses file_get_contents()
and HTTP context. You can pass SSL context options by constructor.
# Without parameters
$client = new Milo\Github\Http\StreamClient;
# Set SSL parameters
$client = new Milo\Github\Http\StreamClient([
'cafile' => '/etc/ssl/trusted.ca.pem',
]);
It is not real client. It is a client wrapper with caching capability. The reason for the client is the rate limiting.
It uses Milo\Github\Storages\ICache interface to store cacheable responses. Default naive implementation is the FileCache.
$cache = new Milo\Github\Storages\FileCache('/tmp');
$client = new Milo\Github\Http\CachedClient($cache);
# Or specify inner client explicitly
$cache = new Milo\Github\Storages\FileCache('/tmp');
$innerClient = new Milo\Github\Http\StreamClient;
$client = new Milo\Github\Http\CachedClient($cache, $innerClient);
# Get the inner client
$client->getInnerClient();
Both clients (CurlClient
and StreamClient
) check a peer CA certificate validity by default. Trusted CA file is included in the project repository.
Client interface has onRequest()
and onResponse()
methods just for debugging purpose.
$client->onRequest(function(Milo\Github\Http\Request $request) {
var_dump($request);
});
$client->onResponse(function(Milo\Github\Http\Response $response) {
var_dump($response);
});