How can I set an API key once and save it within a function?

Relative

I am working on some classes and functions. The functions fetch data from an API which requires an API key. Is it possible to set an API key once and then use it throughout the program?


// Class example
class Airport
{
    public function setClient($appId, $appKey)
    {
        $client = new GuzzleHttp\Client([
            'headers' => array(
                'resourceversion' => 'v4',
                'accept' => 'application/json',
                'app_id' => $appId, // Set this
                'app_key' => $appKey // And this
            )
        ]);
    }
}
// Other file example
require 'classes.php';

$airport = new Airport();
$airport->setClient('xxxxxxxxxxx', 'xxxxxxxx');

// Continue to use other functions without setting the API key again.
MER

You can save them as properties using $this

I'm not sure if you want to reuse the client or the app id/key, but it's pretty much the same idea either way.


// Class example
class Airport
{
    private $appId;
    private $appKey;
    private $client;

    public function setClient($appId, $appKey)
    {
        $this->appId = $appId;
        $this->appKey = $appKey;

        $this->client = new GuzzleHttp\Client([
            'headers' => array(
                'resourceversion' => 'v4',
                'accept' => 'application/json',
                'app_id' => $this->appId, // Set this
                'app_key' => $this->appKey // And this
            )
        ]);
    }

    // New function that uses the client
    public function someOtherMethod()
    {
        $x = $this->client->someMethod();
    }

    // new function that uses the app properties
    public function anotherMethod()
    {
        $x = new Something($this->appId, $this->appKey);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I hide my API key stored within JavaScript?

How can i run a task more than once within the grunt initConfig function

How can I ensure that pytest fixture function is executed only once irrespective of howmany calls within same module

How can i set a "key" argument in a gather function using a variable?

How can I set a value for the key "homepage" without using the API?

How can I execute a function only once?

How can I select multiple html elements of an array and set them all to a function at once?

Can I set a key-value of a object, a call for a function? If so, how can I call it?

How can I save all of my Git stashes differences at once?

How can I save all my booleans at once in onSaveInstanceState?

How can I use sudo within a function?

How can I set Google API Key programmatically? I'm getting "The request is missing a valid API key." Error (403)

How can I set API Key Security Globally on API Gateway using Swagger

In Clojure, how do I sort a set of maps by a key within a value?

In C - Can I initialize an array (and set the size) from within a function?

How can I set the compose key to End?

I have a function within a function, how do I break out of them both at once?

How can I access (and save to my database) nested objects/properties within a JSON array via an API GET request in Ruby on Rails?

How can I set my password for my database user once?

How I can update image of fresco SimpleDraweeView, if it was set once by setImageURI

Netlogo: How can I set timer counting for multiple turtles at once?

How Can I Set a Formula to Only Update Once

How can I pass a parameter into the Chrome Storage get function, and set that parameter as the key?

How can I toggle the Fn (function) key?

Android Q: Can I set the default directory once and save files without file picker?

Using Java API TransmissionWithRecipientArray object, how can I set an element like a key value array ( Sparkpost )

How can I group objects alphabetacally within an array by a specific key?

How can I reference the key in the Pandas dataframes within that dictionary?

How can i Loop in a Json Object Key within Php Values