PHPackages                             webservco/http-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. webservco/http-client

ActiveLibrary[HTTP &amp; Networking](/categories/http)

webservco/http-client
=====================

A PHP component/library.

v1.0.0(1y ago)0317MITPHPPHP ^8.4

Since Jun 22Pushed 1y ago1 watchersCompare

[ Source](https://github.com/webservco/http-client)[ Packagist](https://packagist.org/packages/webservco/http-client)[ Docs](https://webserv.co)[ RSS](/packages/webservco-http-client/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (20)Versions (16)Used By (0)

webservco/http-client
=====================

[](#webservcohttp-client)

A minimalist PHP HTTP Client implementation using cURL.

Test project: [webservco/http-client-test](https://github.com/webservco/http-client-test)

---

Installation
------------

[](#installation)

`composer.json`:

```
{
  "require": {
    "webservco/http-client": "^0"
  }
}
```

---

Usage
-----

[](#usage)

### PSR-18 HTTP client implementation

[](#psr-18-http-client-implementation)

- Regular usage (send a request, receive a response);
- Fully PSR compliant (can be used as a drop-in replacement);

```
// PSR-18: \Psr\Http\Client\ClientInterface
$httpClient = new HttpClient(
    // \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface
    $curlService,
);
$response = $httpClient->sendRequest(
    // PSR-7: \Psr\Http\Message\RequestInterface
    $request,
);
```

### Custom multi processing HTTP client implementation

[](#custom-multi-processing-http-client-implementation)

- Use to send multiple requests in parallel via cURL multi handle;
- Custom implementation, though input and output are PSR compliant;

```
// \WebServCo\Http\Client\Contract\Service\cURL\CurlMultiServiceInterface
$curlMultiService = new CurlMultiService(
    // \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface
    $curlService
);

/** @var array $requests */
$requests = [
    // PSR-7: \Psr\Http\Message\RequestInterface
    1 => $request1,
    // PSR-7: \Psr\Http\Message\RequestInterface
    2 => $request2,
    // etc
];

// Keep a list of handles to be able to link them to each id. key: id, value: handle identifier.
/** @var array $curlHandleIdentifiers */
$curlHandleIdentifiers = [];

foreach ($requests as $request) {
    // Create handle and add it's identifier to the list.
    $handleIdentifier = $curlMultiService->createHandle($request);
    $curlHandleIdentifiers[$releaseId] = $handleIdentifier;
}

// Add more requests if needed
// Create handle and add it's identifier to the list.
$handleIdentifier = $curlMultiService->createHandle($request);
$curlHandleIdentifiers[$releaseId] = $handleIdentifier;

// When ready, execute all requests in parallel
$curlMultiService->executeSessions();

// Retrieve responses

// Use case: consumer keeps track of requests added and needs to be able to identify corresponding responses.
foreach ($curlHandleIdentifiers as $releaseId => $handleIdentifier) {
    $response = $curlMultiService->getResponse($handleIdentifier);

    // Do something with the response
}

// Use case: consumer just needs all responses, no need to link them to the requests.
foreach ($curlMultiService->iterateResponse() as $response) {
    // Do something with the response
}

// Cleanup. After this the service can be re-used, going through all the steps.
$curlMultiService->cleanup();
```

---

Initialization
--------------

[](#initialization)

### `CurlServiceInterface`

[](#curlserviceinterface)

Main service class, needed for any chosen usage.

Dependencies: any PSR-17 implementation can be used.

```
// \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface
$curlService = new CurlService(
    // \WebServCo\Http\Client\DataTransfer\CurlServiceConfiguration
    new CurlServiceConfiguration(
        // enableDebugMode
        true
    ),
    // \WebServCo\Log\Contract\LoggerFactoryInterface
    new ContextFileLoggerFactory(sprintf('%svar/log', $projectPath)),
    // PSR-17: \Psr\Http\Message\ResponseFactoryInterface
    $responseFactory,
    // PSR-17: \Psr\Http\Message\StreamFactoryInterface
    $streamFactory,
);
```

### An example factory using `webservco/http` implementation

[](#an-example-factory-using-webservcohttp-implementation)

```
