Code Monkey home page Code Monkey logo

http-client-curl's Introduction

Simple HTTP cURL client for PHP 7.1+ based on PSR-18

Build Status Code Coverage Scrutinizer Code Quality Total Downloads Latest Stable Version License


Installation

composer require sunrise/http-client-curl

QuickStart

composer require sunrise/http-factory
use Sunrise\Http\Client\Curl\Client;
use Sunrise\Http\Factory\RequestFactory;
use Sunrise\Http\Factory\ResponseFactory;

$client = new Client(new ResponseFactory());
$request = (new RequestFactory)->createRequest('GET', 'http://php.net/');
$response = $client->sendRequest($request);

echo $response->getStatusCode(), PHP_EOL;

cURL options

https://www.php.net/manual/ru/curl.constants.php

$client = new Client(new ResponseFactory(), [
    \CURLOPT_AUTOREFERER => true,
    \CURLOPT_FOLLOWLOCATION => true,
]);

Parallel execution of multiple requests

$requests = [
    (new RequestFactory)->createRequest('GET', 'http://php.net/'),
    (new RequestFactory)->createRequest('GET', 'http://php.net/'),
];

$client = new Client(new ResponseFactory());
$responses = $client->sendRequests(...$request);

foreach ($responses as $i => $response) {
    // note that you can get the response's request by its index...
    echo sprintf('%s => %d', $requests[$i]->getUri(), $response->getStatusCode()), PHP_EOL;
}

Test run

composer test

Useful links

http-client-curl's People

Contributors

eclipxe13 avatar fenric avatar peter279k avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

peter279k

http-client-curl's Issues

Implement set/get curl options

As title, I think we should implement the set and get methods to manipulate the protected $curlOptions.

To prevent overriding the existed curl options for the array $curlOptions argument in Client class constructor, it should check whether the options are set currently.

And the overriding option work is on Client::padCurlOptions method.

Could mark as provide `psr/http-client-implementation`?

This package implements contains in composer.json:

"provide": {
    "php-http/client-implementation": "1.0"
}

Could you please add "psr/http-client-implementation": "^1.0" so your package can satisfy this virtual package?

Thanks for your great work!

Response isn't ready to read

The returned ResponseInterface instance contains a StreamInterface instance that isn't ready to read.

The issue is here:

private function createResponseFromCurlHandle($curlHandle) : ResponseInterface
{
/** @var int */
$statusCode = curl_getinfo($curlHandle, CURLINFO_RESPONSE_CODE);
$response = $this->responseFactory->createResponse($statusCode);
/** @var float */
$requestTime = curl_getinfo($curlHandle, CURLINFO_TOTAL_TIME);
$response = $response->withAddedHeader('X-Request-Time', sprintf('%.3f ms', $requestTime * 1000));
/** @var ?string */
$message = curl_multi_getcontent($curlHandle);
if ($message === null) {
return $response;
}
/** @var int */
$headerSize = curl_getinfo($curlHandle, CURLINFO_HEADER_SIZE);
$header = substr($message, 0, $headerSize);
$response = $this->populateResponseWithHeaderFields($response, $header);
$body = substr($message, $headerSize);
$response->getBody()->write($body);
return $response;
}

The response gets returned after writing to the stream, which means the stream pointer will be at the end of the stream.

After writing to the response body stream (in line 228) you need to rewind the stream, e.g.:

        $response->getBody()->write($body);
        $response->getBody()->rewind(); // ๐Ÿ‘ˆ

I'm using nyholm/psr7, but that shouldn't matter - if your StreamInterface implementation is somehow rewinding the body stream automatically after write (or isn't moving the stream pointer ahead in the first place) it isn't following the PSR-7 spec.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Unsupported composer value

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

composer
composer.json
  • php >=7.1
  • psr/http-client ^1.0
  • psr/http-factory ^1.0
  • psr/http-message ^1.0
  • phpunit/phpunit 7.5.20|9.5.0
  • sunrise/coding-standard 1.0.0
  • sunrise/http-factory 2.0.2

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.