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

dlook's avatar
Level 4

Communicate with JSON receiving API with PHP

Consider this:

We a service that provides API, they have like 7 endpoints and it receives JSON requests.

So far we've been using cURL request to send those, but it get's really messy because almost all of these have like big requests (like creating an Invoice with PayPal API for example, there you have Invoice, then InvoiceItem, Billing containers etc etc). So we basically want to create a SDK (I'm not sure I'm using this correctly) to communicate with this API so instead of having it where you have to get the entire array with all the containers and then json_encode it and curl it we would like to accomplish something like that aforementioned PayPal SDK is doing.

This is how we do it currently, worth to mention that our web app is running on Laravel (our app, not the actual API)

class TestApi {
    // The 'engine' of the api communicator
    public function curlApi($data, $endpoint) {
        $data = json_encode($data);
        $auth = base64_encode(config('api_user').':'.config('api_pw'));
        $headers = array(
            'Content-type: application/JSON',
            'Authorization: Basic '.$auth
        );

        $ch = curl_init();
        $curlConfig = array(
            CURLOPT_URL            => $endpoint,
            CURLOPT_HTTPHEADER     => $headers,
            CURLOPT_POST           => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS     => $data_json,
        );

        curl_setopt_array($ch, $curlConfig);
        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, false);
    }

    // Example of one of the endpoints handlers
    public function testEndpointOne() {
        $data = array(
            "enpointOneRequest" => array(
                "channel" => "Main",
                "id" => 123456,
                "subElementOne" => array(
                    "customerFirstName" => request('first'),
                    "customerLastName" => request('last'),
                    "customerEmail" => request('email'),
                )
            )
        );
        return $this->curlApi($data, 'https://api.example.com/apidir/nameofendpointone')
    }
}

Then we run it like:

$api = new TestApi()
return $api->testEndpointOne();

What we want to accomplish is something like this

use TestApi\ApiStarter;
use TestApi\ApiCustomer;
use TestApi\ApiCustomer\PersonalInfo

...

// Auth here
$apiStart = new TestApiStarter(new ApiAuth($user, $pas));


// Start the correspodning endpoint
$newCustomer = new TestApiCustomer();

// Set root info
$newCustomer->setChannel('Main')
    ->setId(uniqid());

// Start subcontainer
$newCustomerInfo = new TestApiCustomerInfo();

// Set 'customer' info
$newCustomerInfo->setCustomerPersonalInfo(new TestApiCustomerPersonalInfo());

$newCustomerInfo->getCustomerPersonalInfo()
    ->setFirstName(request('first'))
    ->setLastName(request('last'))
    ->setEmail(request('email'));

try {
    $create = $newCustomer->create($apiStart);

    //Save the response locally
}
catch(Exception $exception) {
    log($exception);
}

How do we accomplish that, what's the best approach from performance and coding standpoints?

0 likes
0 replies

Please or to participate in this conversation.