Yii. Add an API Key when connecting to external resource

Started by land_driver, Jul 18, 2022, 04:11 AM

Previous topic - Next topic

land_driverTopic starter

//api.adm.com/v1/ using Yii, you can add an API key in two ways: via a custom header named API-KEY-1 or via a query string parameter named API-KEY-3. According to my understanding, you can accomplish this by using $APIREST.

$ApiKey='API-KEY';
$C1Url='https://api.adm.com/v1/';
$APIREST = new APIREST($C1Url);
$C2Coins= $APIREST->call(
    array('API-KEY-1:'.$ApiKey)
);

But I'm not strong in programming, which is why I need help on how to properly fit into my code.
<?php

return [
    
'adminEmail' => 'adm@adm.net',
    
'urlApi9' => 'https://api.adm.com/v1/',
    
'urlWm'    => 'https://api2.adm.com/',
];
  •  

titris

It's strange that the objects in the JSON you provided have a "name" property. Before proceeding, can we clarify what format the array returned by this method should be in? The function is not very well written, so it would be simpler for me to comprehend its logic and properly rewrite the entire thing.
  •  

natmir

It is not necessary for the API documentation to provide detailed explanations of the internal authentication processes for external users. In fact, it is best practice to omit these details, as it makes it harder for hаckers to exploit vulnerabilities in the API. However, it is crucial to provide instructions on obtaining an API key, authenticating a request, error messages related to incorrect authentication, the sensitivity of authentication information, and the validity period of the access token.

If there are public and private keys, it should be made clear where each should be used, and it should be emphasized that private keys should not be used together. Additionally, if different license levels provide varying access to API calls, these levels should be specified explicitly in the authorization section or elsewhere. Given how important the API Keys section is, and how essential it is for developers to have this information prior to using the API, it should be placed at the beginning of the documentation guide.
  •  

Kovtalo

Based on the provided code snippet and configuration file, it seems that you are using Yii framework and attempting to make an API call to `api.adm.com/v1/` and pass an API key in the request. Here's how you can modify your code to properly include the API key:

```php
<?php

// ...

// Set your API key
$apiKey = 'YOUR_API_KEY';

// Create the API REST instance
$apiUrl = Yii::$app->params['urlApi9'];
$APIREST = new APIREST($apiUrl);

// Make the API call with the API key in the header
$C2Coins = $APIREST->call([
    'API-KEY-1: '.$apiKey
]);

// ...
```

In this modified code, ensure to replace `'YOUR_API_KEY'` with the actual API key you have. It sets the API key and makes the API call using the `API-KEY-1` header.

Additionally, in your configuration file (`config/params.php`), make sure the `'urlApi9'` key is set correctly with the base API URL: `'https://api.adm.com/v1/'`.

Remember to import the necessary class (`APIREST`) and configure any other required dependencies for the API integration.

Note: It's important to properly handle and secure your API keys, as revealing them in publicly accessible code can pose security risks.

Here's an expanded example that demonstrates how you can integrate the API key into your code using Yii:

```php
<?php

// ...

use yii\httpclient\Client;

// ...

// Set your API key
$apiKey = 'YOUR_API_KEY';

// Create the HTTP client
$client = new Client([
    'baseUrl' => Yii::$app->params['urlApi9'],
]);

// Make the API call with the API key in the header
$response = $client->createRequest()
    ->setHeaders([
        'API-KEY-1' => $apiKey,
    ])
    ->send();

// Get the response data
if ($response->isOk) {
    $data = $response->data;
    // Process the response data as needed
} else {
    // Handle the API call failure or error
}

// ...
```

In this example, we are using Yii's built-in HTTP client (`yii\httpclient\Client`) to make the API call. The API key is set as a header in the request using `'API-KEY-1'` as the header name.

After making the API call, you can access the response data (`$response->data`) and process it accordingly. If the API call is unsuccessful (not a 2xx status code), you can handle the failure or error as needed.

Make sure to replace `'YOUR_API_KEY'` with your actual API key, and verify that the `urlApi9` parameter in the configuration file points to the correct API endpoint (`'https://api.adm.com/v1/'`).

Remember to import the necessary classes (`Client`) and configure any other dependencies required for the HTTP client to function properly.
  •