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

thisisthefuture's avatar

Consuming an API - Changing the data - Sending it to another API

First of all: How is this application called?

The user story: There is an API (let's call it API X), where we can fetch/get data from. This data needs to be send to another API (let's call it API Y). But the data is not in a correct way.

API X sends the data in a list and API Y wants an API call for each object seperate.

So API X has this array list:

[articles] => [article 1] => [data], [article 2] => [data]

But API Y wants it in this way (and let's say it wants XML)

< article 1 XML values >

For now there is one big loop build for fetching that data from API X and sending it to API Y.

First of all: How is an application called like that? Is there a go-to way to solve this stuff? What do I need to google for if I want to understand this logic in a better way, like what design patterns to use or what architecture to choose?

Thanks everyone in advance :)

0 likes
3 replies
lostdreamer_nl's avatar

I believe what you're trying to do comes very close to a Enterprise Service Bus: https://en.wikipedia.org/wiki/Enterprise_service_bus

What you would need is:

  1. Some sort of trigger so the ESB knows when to get data from API X (could be a cronjob, or a queue, or anything realy)
  2. a communication protocol between the ESB and API X (and ofcourse the ESB and API Y)
  3. An adapter class that transform the data from API X, into something you can easily work with (associative array / json)
  4. Another adapter class that can transform that structure (associative array / json) into the structure API Y wants it to be.
  5. Maybe an event system to make sure that the classes you create don't have to be interconnected to much.

So the working of the ECB could become something like:

  1. get data from API X (create a separate class for communicating with ApiX)
  2. use an adapter class (ApiXAdapter) to transform the data into an array you can work with
  3. trigger a ApiXDataWasReceived event, passing in the array of data
  4. implement a listener (SendDataToApiY) that will listen for this event which will:
    1. Use another adapter (ApiYAdapter) to transform the array into the XML API Y wants
    2. call another class (ApiY) that communicates with API Y to send the data

Now if you ever need to let another API Z (SOAP ?) know about this data, you just implement another listener, create another adapater class to change the array data into the structure for API Z, and have it send it to the correct API.

1 like
thisisthefuture's avatar

What an insane amount of information! Thank you very much! I was looking for this a very long time. Especially to what it is called.

The trigger is done already with a cronjob indeed and then checks with a variable to get all the changed articles.

How would you let this all work together? Would you make one big loop out of this? Or put these events in a quee? I'm curious about how I would approach that. It could be a pretty big loop. And what is the approach to save or write down that things are succesful?

So far I have (from your info):

  • Make an adapter

  • Trigger an Event

  • Listen to That event

  • Make an adapter that transforms my code in XML (or any other data what I want)

  • Call class that communicates: Is there a good design pattern for this?

Sorry for all the questions but I'm trying to step up my game and really get into software architecture and design patterns.

lostdreamer_nl's avatar
Level 53

The flow would be: 1 command that get's the data from ApiX, it then loops over the dataset and for each block of data, it triggers another command (SendDataToApiY)

Each class should only have 1 responsibility: 1 class that communicates with API X 1 class that communicates with API Y 1 class that only transforms data from / to API X's standard to your internal one 1 class that only transforms data from / to API Y's standard to your internal one

and 1 class to bind this all, which is the command that you run to get the data from API X, transform it into the internally used data structure, and runs a command to send that data to Api Y.

OK, some quick pseudo code to get you up and running:

Create 2 classes for communicating with API X and Y App\Apis\ApiX.php:

class ApiX 
{
    private $url = 'https://endpoint.to/api';

    public function sendData($data) 
    {
        // use cURL or something to actually send the data
        // have some error checking here (throwing exceptions when things go wrong)

        // return true on success
        return true;
    }

    public function getData() 
    {
        // use cURL or something to actually get the data
        // throw Exceptions when things go wrong
        // return $data on success
        return $result;
    }
}

App\Apis\ApiY.php:

class ApiY
{
    private $url = 'https://another.endpoint.to/api';

    public function sendData($data) 
    {
        // use SOAP or something to actually send the data
        // throw Exceptions when things go wrong
        // return true on success
        return true;
    }

    public function getData() 
    {
        // use SOAP or something to actually get the data
        // throw Exceptions when things go wrong
        // return $data on success
        return $data;
    }
}

Create some adapter classes App\Adapters\ApiXAdapter.php App\Adapters\ApiYAdapter.php

class ApiXAdapter 
{
    public function transformFromArray(array $data) 
    {
        // might be implemented, doesnt need to be if you only GET data from this API
    }

    public function transformToArray($data) 
    {
        // $data would be the result you get from ApiX, let's assume it's json
        $data = json_decode($data, true);
        // dont just return the array as you got it form API X, make sure that you always know which structure your internally used dataset uses.
        return [
            'name' => $data['how-they-call-this-key'],
            'address' => $data['how-they-call-this-key'],
            'zipcode' => $data['how-they-call-this-key'],
        ];
    }
}
class ApiYAdapter 
{
    public function transformFromArray(array $data) 
    {
        // we know that the keys name, address and zipcode will exist
        // but API Y wants SOAP XML instead of an array
        // [ do some magic stuff to convert the array into XML]
        return $xml;
    }

    public function transformToArray($data) 
    {
        // might be implemented, doesnt need to be if you only SEND data to this API
    }
}

Create a Job class: SendDataToApiY

class SendDataToApiY implements ShouldQueue
{
    private $data;
    use InteractsWithQueue, Queueable, SerializesModels;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function handle()
    {
        $api = app()->make(ApiY::class);
        $adapter = app()->make(ApiYAdapter::class);
        Try {
            // $this->data is the array as gotten from the ApiXAdapter
            $data = $adapter->transformFromArray($this->data);
            // $data now is the exact data you want to send to $apiY
            $api->sendData($data);
        } catch(\Exception $e) {
            // something went wrong in the adapter or apiY, log this message
            \Log::error('SendDataToApiY Error: '.  $e->getMessage());
        }
    }
}

Create a command class: GetApiXData

class GetApiXData extends Command
{
    public function handle()
    {
        $api = app()->make(ApiX::class);
        $adapter = app()->make(ApiXAdapter::class);
        $data = $api->getData();
        foreach($data as $row) {
            // $row is the data as gotten from API X
            $row = $adapter->transformToArray($row);
            // $row is now the data as transformed by the ApiXAdapter
            dispatch(new SendDataToApiY($row));
        }
    }
}

In this way I'm not using any events or listeners, but just the queue system. You can trigger the GetApiXData from an artisan command. It will get the data from Api X, transform it into something usable and for each block of data it got, it will dispatch another job onto the queue (SendDataToApiY)

Which will then transform the data back into something ApiY can handle, and send it on it's way.

I hope you get the gist of it.

If you want to learn about design patterns, I can highly recommend Php Objects, Patterns And Practice from Matt Zandstra.

(on google you can even find some PDF versions at some uni websites)

1 like

Please or to participate in this conversation.