Monday, May 22, 2017

How to send bulk data through Guzzle?

I am implementing an API that needs to receive data from my database. The data comes from my 'Products' table. I organized the products that I want to send through Guzzle.

public function post()
    {
      $product = Product::where('erp_status', '=', 1)->limit(5)->offset(0)->get();
        $client = new Client([
            // Base URI is used with relative requests
            'base_uri' => 'http://httpbin.org',
            // You can set any number of default request options.
            'timeout'  => 2.0,
        ]);

And right after I created a foreach to send the data through the URL.

foreach($product as $prod){
          set_time_limit(0);
          $r = $client->request('POST', 'https://api.mercadolibre.com/items?access_token=XXXXXXXXXXXXXXXXXXX', [
              'json' => [
                  'title' => $prod->description->erp_name,
                  'category_id' => 'MLB46511',
                  'price' => 10,
                  'currency_id' => 'BRL',
                  'available_quantity' => 10,
                  'buying_mode' => 'buy_it_now',
                  'listing_type_id' => 'gold_special',
                  'condition' => 'new',
                  'description' => 'Teste',
                  'pictures' => [
                      ['source' => $prod->image->erp_image]
                  ]
              ]
          ]);
      }
      return view('home');
}

But every time I run. It returns the message:

cURL error 28: Operation timed out after 2012 milliseconds with 0 out of 0 bytes received (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

My variable $product have 5 thousand rows.

Does Guzzle support this amount? Is it possible to use the guzzle in this case or what is the pure PHP script?

Any suggestion?



via Vinicius Rosa

Advertisement