Skip to content

HTTP clients

milo edited this page Sep 30, 2014 · 9 revisions

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.

CurlClient

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,
]);

StreamClient

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',
]);

CachedClient

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();

SSL certificates

Both clients (CurlClient and StreamClient) check a peer CA certificate validity by default. Trusted CA file is included in the project repository.

Debugging

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);
});
Clone this wiki locally