Skip to content

HTTP clients

Miloslav Hůla edited this page May 14, 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.

# Without parameters
$client = new Milo\Github\Http\CurlClient;

# Set some SSL parameters
$client = new Milo\Github\Http\CurlClient([
    CURLOPT_SSL_VERIFYPEER => TRUE,
    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 some SSL parameters
$client = new Milo\Github\Http\CurlClient([
    'verify_peer' => TRUE,
    '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 wrapped client explicitly
$cache = new Milo\Github\Storages\FileCache('/tmp');
$realClient = new Milo\Github\Http\StreamClient;
$client = new Milo\Github\Http\CachedClient($cache, $realClient);

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