API URLs and Parameters to be configurable in Drupal/PHP

55 Views Asked by At

I have a drupal 8 website and have a lot of integrations(using curl) in custom modules.

The URLs of these APIs and its parameters need to be configurable by an admin panel.

What is the best way to do that? Building a custom table and a custom form to save it or is there a specific module or create an entity type in admin configuration?

Here is my code:

      $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => "**https://test?parameter1=test&parameter2=test"**,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_HTTPHEADER => [
                "Content-Type: application/json",
                "accept: application/json",
                **"Authorization: Basic XXX"**
            ],
        ]);
1

There are 1 best solutions below

0
On

You should create a Configuration Form to enter config values, and then Configuration API will save those values in config table.

When need to retrieve config values you can use:

$config = \Drupal::config('<your_config_name>');
$value = $config->get('<your_key>');