Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make create batch request in Guzzle6?

I need to send multiple requests so I want to implement a batch request.

How can we do it in Guzzle6?

Using the the old way:

$client->send(array(
    $client->get($courses),  //api url
    $client->get($job_categories), //api url
)); 

is giving me the error:

GuzzleHttp\Client::send() must implement interface Psr\Http\Message\RequestInterface, array given
like image 837
RahulG Avatar asked Jun 24 '15 06:06

RahulG


People also ask

How do I send a POST request with GuzzleHttp?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);

What is GuzzleHttp?

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...

How do you send a header on guzzle?

// Set various headers on a request $client->request('GET', '/get', [ 'headers' => [ 'User-Agent' => 'testing/1.0', 'Accept' => 'application/json', 'X-Foo' => ['Bar', 'Baz'] ] ]); Headers may be added as default options when creating a client.

What is guzzle in laravel?

Laravel provides an expressive, minimal API around the Guzzle HTTP client, allowing you to quickly make outgoing HTTP requests to communicate with other web applications. Laravel's wrapper around Guzzle is focused on its most common use cases and a wonderful developer experience.


1 Answers

try something like this

    $client = new Client();
    foreach ($links as $link) {
        $requests[] = new Request('GET', $link);
    }

    $responses = Pool::batch($client, $requests, array(
        'concurrency' => 15,
    ));

    foreach ($responses as $response) {
         //do something
    }

don't forget

use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
like image 162
Kirill Sulimovsky Avatar answered Sep 22 '22 22:09

Kirill Sulimovsky