davidtsadler / ebay-sdk-examples

Several examples of using the eBay SDK for PHP
http://devbay.net
Apache License 2.0
184 stars 100 forks source link

Explanation how to store credentials in another place #49

Closed cukabeka closed 7 years ago

cukabeka commented 7 years ago

Hi,

I wonder how to tell my script to use a configuration file that is in another place than default.

CredentialsProvider.php

    /**
     * Provider that creates credentials using an ini file stored in the
     * current user's home directory.
     *
     * @param string|null $profile Profile to use. Defaults to "default".
     * @param string|null $filename If provided, uses a custom filename rather than
     * looking in the home directory for the current user.
     *
     * @return callable
     * @throws \InvalidArgumentException
     */
    public static function ini($profile = null, $filename = null)
    {
        $filename = $filename ?: (self::getHomeDir() . '/.ebay_sdk/credentials');

To be more precise, it would help a lot to know how to pass parameters to this ini function so that I could use credentials from any place relative to the current script call.

davidtsadler commented 7 years ago

You can create a credentials provider that loads from an ini file in any directory by calling CredentialsProvider::ini. The first parameter is the profile name in the ini file and the second is the path to it.

use \DTS\eBaySDK\Credentials\CredentialsProvider;

$provider = CredentialsProvider::ini('default', '<path_to_your_ini_file>');

$service = new Services\TradingService([
    'credentials' => $provider,
    'siteId' => Constants\SiteIds::US,
]);

Because it can be expensive to keep accessing a file you can cache the results in a memozie function. This way the file is only loaded once.

use \DTS\eBaySDK\Credentials\CredentialsProvider;

$provider = CredentialsProvider::ini('default', '<path_to_your_ini_file>');

// Cache the results in a memoize function to avoid loading and parsing
// the ini file every time a service is instantiated.
$provider = CredentialsProvider::memoize($provider);