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

Ilya_user's avatar

The best way to handle multiple external api options in laravel

I have a form where a user picks a post office for his delivery, for now it can handle only one post office and one external api. I'm thinking what can be the best way to add multiple post offices with their own unique external api's? I know that i can do an if statement for each of them, it's alright if I only have a couple of post office selections but if I'll have lets say 15 of them a code is gonna look very big and messy. What are some suggestions? Thanks in advance.

public function post(Request $request){

        $details =  [
            "client_name" => $request->input('customer_name'),
            "phone_number" => $request->input('phone_number'),
            "email" => $request->input('email'),
            "sender_address" => $request->input('sender_address'),
            "delivery_address" => $request->input('delivery_address'),
            "package_size" => $request->input('width') . 'x' . $request->input('height')
            . 'x' . $request->input('lenght') . 'x' . $request->input('weight'),
          ];

          $response = Http::post('post.test/api/delivery', $details);
          $result = $response->json();
          dd($result);
    }
0 likes
5 replies
MohamedTammam's avatar

Send the data as an array, then use that array.

$results = [];

foreach($request->clients as $client) {
  $details =  [
            "client_name" => $client('customer_name'),
            "phone_number" => $client('phone_number'),
            "email" => $client('email'),
            "sender_address" => $client('sender_address'),
            "delivery_address" => $client('delivery_address'),
            "package_size" => $client('width') . 'x' . $client('height')
            . 'x' . $client('lenght') . 'x' . $client('weight'),
  ];

  $response = Http::post('post.test/api/delivery', $details);
  $results[] = $response->json();
}

dd($results);

Couple of notes:

  1. You need to add validation

  2. In most cases 15 HTTP request your server makes per request is a huge number, consider using corn jobs if you can.
Ilya_user's avatar

@MohamedTammam thanks but i probably didnt ask the question the right way. I don't have to change anything regarding the data that a client provides me with in the $details, i just wanna check what post office client chooses in the form and based on that use different url since every post office will have their own unique url, here's that part of the code: $response = Http::post('basically here the url should be different depending on what post office a client chooses', $details);

martinbean's avatar

@ilya_user When you say different post offices, do you mean different branches of the same post office company? Or entirely different post office companies, with entirely different APIs?

martinbean's avatar
Level 80

@Ilya_user OK. So in that case, you’ll need to write separate classes to interact with each post office company’s API. You’ll make your life easier if you make these classes conform to a common interface. Then when the end user picks a post office, you can use the corresponding implementation to do whatever logic you need to do:

interface PostOfficeService
{
    public function getDeliveryQuote(Address $address, Dimensions $package): Money;
}
class PostOfficeOneService implements PostOfficeService
{
    public function getDeliveryQuote(Address $address, Dimensions $package): Money
    {
        // Code here to get quote from post office #1
        // to get delivery quote for given address and package dimensions...
    }
}
class PostOfficeTwoService implements PostOfficeService
{
    public function getDeliveryQuote(Address $address, Dimensions $package): Money
    {
        // Code here to get quote from post office #2
        // to get delivery quote for given address and package dimensions...
    }
}

You can have some sort of manager class that returns the correct implementation based on the user’s selection, and calls the relevant method:

$service = $postOfficeService->getServiceFor($request->input('post_office'));

$quote = $service->getDeliveryQuote($address, $dimensions);

As you can see, if you use a single interface for all services, then your code doesn’t need to change depending on which post office is selected. You can also add new implementations, change existing implementations if a post office updates its API, etc.

You’ll see I’ve also used objects for things like Address and Dimensions. These would be classes in your application to make things consistent. Pretty much DTOs. I also standardise the output to just be a Money instance, rather than each service returning results in different shapes and formats depending on which service was called.

1 like

Please or to participate in this conversation.