arhitector/http-curl-client

cURL client for PHP-HTTP

1.3.0 2016-03-25 16:17 UTC

This package is auto-updated.

Last update: 2024-12-13 02:41:42 UTC


README

Latest Version Software License Build Status Code Coverage Quality Score Total Downloads

The client for sending requests of PSR-7. The cURL client use the cURL PHP extension which must be activated in your php.ini.

Differences from https://github.com/php-http/curl-client

  • Support custom method, such as "MOVE", "COPY", "PROPFIND", "MKCOL" etc. (it is actual by operation with WebDav)
  • Fixed supports transmission of big bodies (upload and download).
  • Full support of native streaming filters, such as 'zlib.deflate' etc.
  • Fixed bug with queues in async request.
  • Doesn't lead to hangup of the server, in case of the inexact size of a flow.
  • More friendly api.
  • Inheritance support.
  • You can use options "CURLOPT_READFUNCTION" and "CURLOPT_WRITEFUNCTION".
  • Support of an native option "CURLOPT_FOLLOWLOCATION". (if you need support cookie with redirect - use "CURLOPT_COOKIEJAR" and "CURLOPT_COOKIEFILE")
  • Support send "PSR-7 Response".
  • More correct support of header "Expect"

Install

Via Composer

$ composer require arhitector/http-curl-client:~1.3

Quickstart

For example with Zend/Diactoros:

use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\StreamFactory\DiactorosStreamFactory;

$client = new Mackey\Http\Client\Curl\Client(new DiactorosMessageFactory(), new DiactorosStreamFactory(), [
  // array, set default curl options, if you need
  CURLOPT_SSL_VERIFYPEER => false
]);

Send request

$request = new Zend\Diactoros\Request('<url>', 'GET');

$response = $client->sendRequest($request, [
  // curl options for 1 request, if you need
  CURLOPT_FOLLOWLOCATION => true
]);

Send async request

$request = new Zend\Diactoros\Request('<url>', 'GET');

$promise = $client->sendAsyncRequest($request);
$promise->then(
  function (ResponseInterface $response) {
    // The success callback

    return $response;
  },
  function (\Exception $exception) {
    // The failure callback

    throw $exception;
  }
);

// other request
// $promise = $client->sendAsyncRequest($request);

try {
    $response = $promise->wait();
} catch (\Exception $exception) {
    echo $exception->getMessage();
}

Send response

// example just send file in output
$stream = new \Zend\Diactoros\Stream('file/to/send.txt');
$response = new \Zend\Diactoros\Response($stream, 200);

$client->sendResponse($response);

// or send for download
$response = $response->withHeader('Content-Description', 'File Transfer')
    ->withHeader('Content-Type', 'application/octet-stream')
    ->withHeader('Content-Disposition', 'attachment; filename=filename.txt')
    ->withHeader('Content-Transfer-Encoding', 'binary');

$client->sendResponse($response);

Documentation

Please see the official documentation.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please contact us at security@php-http.org.

License

The MIT License (MIT). Please see License File for more information.