cobwebinfo / shrek-api-client
The client for Cobweb information's SHREK API.
Requires
- php: >=5.5
- asika/http: ~1.0
- guzzlehttp/guzzle: ~6.0
- league/oauth2-client: ~1.4
- mockery/mockery: ^0.9.5
- symfony/yaml: ^3.1
This package is not auto-updated.
Last update: 2024-12-16 15:49:29 UTC
README
This library can be used to pull data from the various public endpoints provided by the SHREK API. The client has been created with ease of use in mind, simply provide your API ID and private key and use the instructions below to proceed.
##Compatibility
Installation
The suggested installation method is via composer:
php composer.phar require "cobwebinfo/shrek-api-client:1.2.*"
Requesting access to the API.
Please contact Cobwebinfo at @ enquiries@cobwebinfo.com to access an API key.
Usage
The ShrekServiceProvider class provides a neat wrapper for instantiating the various clients needed to access the API. You can manually instantiate the clients if you do not wish to use it, however.
You can get an instance as follows:
$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([ 'client_id' => YOUR ID, 'client_secret'=> YOUR SECRET ]);
I would suggest adding this as a singleton to your service container. If you use Laravel for example, you could do the following:
$this->app->singleton(\Cobwebinfo\ShrekApiClient\ShrekServiceProvider::class, function() { return new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([ 'client_id' => YOUR ID, 'client_secret'=> YOUR SECRET ]); });
The array passed to the provider is used for configuration. To see the available options, refer to the Config.yaml file.
Once you have a provider instance, you can access the various clients as follows:
$keywordClient = $provider->getKeywordClient();
You can then access data from the API as follows:
try { $response = $keywordClient->paginate(1, 4, []); } catch(IdentityProviderException $e) { var_dump('Authentication error: ' . $e->getMessage()); } if($response->wasSuccessful()) { foreach($response->body['data']['items'] as $key => $keyword) { echo "<h3> $key </h3>"; var_dump($keyword); } }
Caching
Please note: By default the app uses the 'NullStore' cache class. This is an implementation of the null object pattern, and as you may have guessed does not cache anything. If you intend to use this method, you will need to implement your own caching to avoid hitting API limits. Alternatively, if your application supports APC or memcache, you can use one of the inbuilt classes to handle caching automatically. To do so, use the config below:
$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([ 'client_id' => YOUR ID, 'client_secret'=> YOUR SECRET, 'cache_driver' => 'memcache' OR 'apc' ]);
If you wish to roll your own cache implementation then create a new class which uses the 'Cobwebinfo\ShrekApiClient\Cache\Contracts\Store' interface and pass the fully qualified name into the Provider, as follows:
$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([ 'client_id' => YOUR ID, 'client_secret'=> YOUR SECRET, 'cache_driver' => '\Your\Namespace\ClassName' ]);
Http Clients
By default Guzzle is used as the HTTP client. If you prefer not to use guzzle, then an alternative implementation is provided. To use this, provide the following config:
$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([ 'client_id' => YOUR ID, 'client_secret'=> YOUR SECRET, 'http_client' => 'asika' ]);
As with caching, you can also roll your own HTTP client should you so choose. Simply create a new class implementing the 'Cobwebinfo\ShrekApiClient\Support\HttpRequester' interface and pass in the full qualified name, as follows:
Please note: The class should return a '\Psr\Http\Message\ResponseInterface' instance.
$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([ 'client_id' => YOUR ID, 'client_secret'=> YOUR SECRET, 'http_client' => '\Your\Namespace\ClassName' ]);
Config
The package provides a Yaml reader sħould you want to store your client id, client secret or other config in a yaml file. It works as follows:
Yaml::parse(file_get_contents(__DIR__ . '/config.yaml'));;
The above would return an associative array, which you could then pass into the ShrekServiceProvider.
Todo
- Clear/bypass cache?
- Packagist