PHP code example of cbaconnier / laravel-http-pool-callback
1. Go to this page and download the library: Download cbaconnier/laravel-http-pool-callback library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cbaconnier / laravel-http-pool-callback example snippets
use GuzzleHttp\Promise\PromiseInterface;
use Cbaconnier\HttpPool\HttpPoolAware;
use Cbaconnier\HttpPool\HasHttpPool;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
class InvoiceRepository implements HttpPoolAware
{
use HasHttpPool;
public function findAsync(int $id): PromiseInterface
{
$promise = $this->http()->get("https://example.com/invoices/{$id}");
// Register the callback that will be executed on the response
$this->onPromiseResolved(function (Response $response) {
return new InvoiceDto($response->getBody()->getContents());
});
return $promise;
}
public function find(int $id): InvoiceDto
{
// You can still use the repository without async requests as well
$response = $this->http()->get("https://example.com/invoices/{$id}");
return new InvoiceDto($response->getBody()->getContents());
}
// Not necessary but here to demonstrate a common use case
// Then use $this->client()->get(...) instead of $this->http()->get(...)
public function client(): PendingRequest
{
return $this->http()
->withHeaders([
'Authorizations' => 'Bearer ...'
])
->withOptions([
// ..
]);
}
}
use Cbaconnier\HttpPool\Facades\HttpPool;
$pool = HttpPool::runAsync([
'invoice' => $invoiceRepository->async(fn (InvoiceRepository $repository) => $repository->findAsync(123)),
'client' => $clientRepository->async(fn (ClientRepository $repository) => $repository->findAsync(123)),
]);
$pool->getResponses(); // Returns ['invoice' => Response, 'client' => Response]
$pool->getResolved(); // Returns ['invoice' => InvoiceDto, 'client' => ClientDto]
// You can also use macro instead of facade
Http::runAsync([ ... ])->getResolved();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.