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

CookieMonster's avatar

how to optimize multiple external api calls in controller?

I have to make few api calls in my controller method, namely getting the first call is to get the response service ID from the external service and then pass it to the request in the second api call(to create order) which gives the response order number where I need to pass it to the request to third call(make order payment) and receive the success/failure response.

    //get the rate checking to obtain the service id from EasyParcel
        
            $postparam_rate = array(
                'api'	=> config('easyparcel.key'),
                'bulk'	=> array(
                array(
                'pick_code'	=> $hub->postcode,
                'pick_state'	=> $hub->state,
                'pick_country'	=> 'MY',
                'send_code'	=> $request->postcode,
                'send_state'	=> $request->state,
                'send_country'	=> 'MY',
                'weight'	=> $request->weight,
                'width'	=> '',
                'length'	=> '',
                'height'	=> '',
                'date_coll'	=> '',
                ),
                ),
                'exclude_fields'	=> array(
                'rates.*.pickup_point',
                )
                );

                $url = "https://demo.connect.easyparcel.my/?ac=EPRateCheckingBulk";

                $response = Http::asForm()->post($url,$postparam_rate);

                $collection = collect($response->json()['result'][0]['rates']);
                
                $filtered = $collection->where('courier_id',$rate_id->courier->courier_id)->first();
                
                if(!$filtered){
                    return redirect()->route('create.order.failed')
                    ->with(['errorMessage' =>'This courier is unavailable at the moment. Please select other courier.'] );
                }

            //dd($filtered['service_id']);

        // Create order in Easy Parcel
            $postparam = array(
                'api'	=> config('easyparcel.key'),
                'bulk'	=> array(
                array(
                'weight'	=> $request->weight,
                'width'	=> '',
                'length'	=> '',
                'height'	=> '',
                'content'	=> $request->content,
                'value'	=> $request->value_content,
                'service_id'	=> $filtered['service_id'],
                'pick_point'	=> '',
                'pick_name'	=> $sender->name,
                'pick_company'	=> '',
                'pick_contact'	=> $request->sender_contact_num,
                'pick_mobile'	=> '',
                'pick_addr1'	=> $hub->address,
                'pick_addr2'	=> '',
                'pick_addr3'	=> '',
                'pick_addr4'	=> '',
                'pick_city'	=> $hub->city,
                'pick_state'	=> $hub->state,
                'pick_code'	=> $hub->postcode,
                'pick_country'	=> 'MY',
                'send_point'	=> '',
                'send_name'	=> $request->recipient_name,
                'send_company'	=> '',
                'send_contact'	=> $request->recipient_contact_number,
                'send_mobile'	=> '',
                'send_addr1'	=> $request->recipient_address,
                'send_addr2'	=> '',
                'send_addr3'	=> '',
                'send_addr4'	=> '',
                'send_city'	=> $request->city,
                'send_state'	=> $request->state,
                'send_code'	=> $request->postcode,
                'send_country'	=> 'MY',
                'collect_date'	=> $request->delivery_date,
                'sms'	=> '0',
                'send_email'	=> $request->recipient_email,
                'hs_code'	=> '',
                'REQ_ID'	=> '',
                'reference'	=> '',
                ),
                ),
                );

                $url = "https://demo.connect.easyparcel.my/?ac=EPSubmitOrderBulk";

                $response = Http::asForm()->post($url,$postparam);
                
                if($response['result'][0]['status'] !='Success'){
                    dd($response['result'][0]['remarks']);
                
                }
            
                Log::info($response->json());
                // dd($response->json());

                // return $response;

                //Make order payment in Easy Parcel

            $postparam_order_payment = array(
                'api'	=> config('easyparcel.key'),
                'bulk'	=> array(
                array(
                'order_no'	=> $response['result'][0]['order_number'],
                ),
                ),
                );

                $order_payment_url = "https://demo.connect.easyparcel.my/?ac=EPPayOrderBulk";

                $order_payment_response = Http::asForm()->post($order_payment_url,$postparam_order_payment);
                Log::info($order_payment_response);
                //dd($order_payment_response->json());
                //check payment order is fully paid
                
                if($order_payment_response['result'][0]['messagenow'] == 'Insufficient Credit'){
                    return redirect()->route('create.order.failed')
                    ->with(['errorMessage' =>'Insufficient credit in Easy Parcel account. Please contact support for help.'] );
                }

All this works but I can imagine it can be taxing and make my system run with slower load times when it hits this method. Is there any way to optimize this?

0 likes
1 reply
automica's avatar

If you are worried about possible performance issues with hitting your api multiple times, then you’ll want to get some metrics on how long this is taking (which will allow you to optimise your backend).

Either drop some Log::info() into your methods at appropriate point or install laravel telescope to and watch the logs from there.

https://github.com/laravel/telescope

Please or to participate in this conversation.