How to send big data to API in laravel?

  guzzle, http, laravel, php
public static function send()
    {
        $products = Product::where('active', 1)->get(['id_product', 'price', 'reference'])->load(['images', 'lang']);
        // loop for all products and sending to API
        $number_of_products = count($products);
        $start = 0;
        $end = 70;
        for ($a = 0; $a < ceil($number_of_products/70); $a++) {
            for ($i = $start; $i < $end; $i++) {
                // loop for all images in product
                $count = count($products[$i]->images);
                for ($k = 0; $k < $count; $k++) {
                    $id = (string)$products[$i]->images[$k]->id_image;
                    $products[$i]->images[$k]->id_image = 'https://somesite.com/img/p';
                    $length = strlen($id);
                    // loop for every digit of id_image
                    for ($d = 0; $d < $length; $d++) {
                        $products[$i]->images[$k]->id_image = $products[$i]->images[$k]->id_image . '/' . $id[$d];
                    }
                    $products[$i]->images[$k]->id_image = $products[$i]->images[$k]->id_image . '/' . $id . ".jpg";
                    $images[] = $products[$i]->images[$k]->id_image;
                }
                $products[$i]->images[0] = $images;
                $product = [
                    'code' => $products[$i]->reference,
                    'name' => $products[$i]->lang->name,
                    'price' => round(($products[$i]->price) * 13200, -2),
                    'quantity' => 1,
                    'description' => $products[$i]->lang->description,
                    'images' => $products[$i]->images[0],
                    'url' => 'https://avtech.uz/ru/' . $products[$i]->id_product . '-' . $products[$i]->lang->link_rewrite . '.html',
                ];
                $array_of_products[] = $product;
            }
            // Send 50 products to API
            $response = Http::withToken('token')->post('https://example.com/upload', [
                'company_id' => ******,
                'products' => $array_of_products
            ]);
            if ($response->failed()) {
                Log::channel('send_products_to_pc')->info('Failed: ', [$response]);
            } else {
                Log::channel('send_products_to_pc')->info('Success: ', [$response]);
            }
            $start = $start + 70;
            $end = $end + 70;
        }
}

HELP PLEASE! What and how can I make this function work successfully.
Getting "504 Gateway Time-out" – in Postman
"Allowed memory size of 134217728 bytes exhausted" – in laravel.log.
I don’t want to increase memory_limit because memory leak might happen, and my script is not able to send 1300 products in 60 secs. In logs I can see that only 500-600 products are sent and then above mentioned errors are given.

Source: Laravel

Leave a Reply