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

Conixs's avatar

Multiple post to 3rd party API

Hello everyone, I have an API that received id's for each client selected and post one by one to a 3rd party API as below:

$client = \App\Client::find($request->client_id);
$clients = collect([$client]);

if(!$clients) {
            return response()->json([
                'message'   => 'Records not found'
            ], 404);
}

foreach ($clients as $client) {
            $data = $request->all();

            $validator = Validator::make($data, [
                'external_number' => 'required',
                'total_value' => 'required',
                'fine_percent' => 'sometimes|numeric|max:10',
                'interest_percent' => 'sometimes|numeric|max:0.19'
            ]);

            if($validator->fails()) {
                return response()->json([
                    'message'   => 'Validation Failed',
                    'errors'    => $validator->errors()->all()
                ], 422);
            }

            $response = $http->post();
        if($response == 'Success')
                return response()->json(['message' => 'Success!'], 200);
            else
                 return response()->json(['message' => 'Error!'], 422);
}

Whats is the best approach to lead with it?

0 likes
3 replies
bobbybouwmann's avatar
Level 88

Well, depending on how many clients you select, the longer the request will take, right? You need to send all the requests to the third party API before returning a response to the user.

I would personally set up a queue and push a job on there for each client to send the request to the 3rd party API. This way your user gets a fast response and you can handle all the 3rd party stuff later.

bobbybouwmann's avatar

It is even better if the 3rd party would accept a batch of clients.

Conixs's avatar

The 3rd party API didn't accept it

Please or to participate in this conversation.