Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kamr's avatar
Level 1

can't send 1300 products to API

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.

0 likes
9 replies
SilenceBringer's avatar
Level 55

@kamr additionally to @sinnbeck answer I can also suggest to grab products partially (as far as you send chunks by 70 items). You do not need to load all products with relationships at once. Load just 70, send it, and then load next 70

1 like
kamr's avatar
Level 1

@SilenceBringer I can grab 70 products like this: "$products = Product::paginate(70)" But How can I grab next 70 and next 70...

Please or to participate in this conversation.