Pedro Cordeiro
Pedro Cordeiro

Reputation: 2125

Dispatching asynchronous http requests using Symfony2

I'm creating something that looks like an RSS reader. People set up subscriptions (from other websites), and I fetch content from them.

I'm working with symfony2 and am currently facing a problem: how do I dispatch, asynchronously, http requests to many urls using symfony2? I know it can be done using cURL, but I'd like to think there's already a bundle for that. I've checked krisswallsmith/buzz and sensio/buzz, but they are poorly documented, apparently outdated (sensio/buzz still uses the vendor script to be installed) and potentially don't allow me to dispatch requests asynchronously - I wouldn't know, as I said, they are poorly documented.

Is there a bundle? If so, which one? If not, what technique should I use to achieve my goal? Should I create a separate bundle just to handle the requests? Should I go inside my controller and write some ugly cURL stuff within my actions? Should I create a service to handle the dispatches?

Upvotes: 1

Views: 1535

Answers (1)

AMK
AMK

Reputation: 836

I think Guzzel is the most famous library for PHP that allows you to send synchronous and asynchronous requests: https://docs.guzzlephp.org/en/stable/

A sample code of async call:

$request = new \GuzzleHttp\Psr7\Request('GET', 'YOUR_URL');
$promise = $client->sendAsync($request)->then(function ($response) {
    echo 'I completed! ' . $response->getBody();
});

Upvotes: 1

Related Questions