Wallogit.com
2017 © Pedro Peláez
League of Legends PHP API Client
This client use the API server provided by : https://github.com/EloGank/lol-php-api, (*1)
In the require key :, (*2)
``` json { "require": { "elogank/lol-php-api-client": "1.0.*@dev" } }, (*3)
Then, in a CLI window : php composer.phar update "elogank/php-lol-api" #### Composer what ? Ok, here a fast lesson about Composer. Composer is a PHP CLI dependency manager. Download Composer in your project root directory, here : https://getcomposer.org/download/ Then, you'll have a new file : `composer.phar`. Now create a `composer.json`, and read this brief documentation : https://getcomposer.org/doc/00-intro.md Finally, install this repository ! ## How to use The `Client` object need three parameters, and two as optionals : * `$host` the server host IP address * `$port` the server port * `$format` the default output format * `$throwException` (optional, default: true) if true, an `ApiException` will be throw on error and the response won't contain the first array level which contain "success" & "result"/"error" keys. * `$timeout` (optional, default: your php.ini "default_socket_timeout" configuration) the connection timeout, in second. In short : ``` php // Declare your client and the configuration $client = new \EloGank\ApiClient\Client('127.0.0.1', 8080, 'json'); // Do your API request try { $response = $client->send('EUW', 'summoner.summoner_existence', ['Foobar']); } catch (\EloGank\ApiClient\Exception\ApiException $e) { // error var_dump($e->getCause(), $e->getMessage()); }
The send() method have three parameters and one optional :
* region it's the client region short name (EUW, NA, ...). Make sure there is a registered client for the selected region in your API !
* route the API route, in short it's the "controller_name.method_name"
* parameters it's the route parameters, it's an array
* format (optional) if you need a specific format for a specific route (see the API configuration documentation for available formats), (*4)
This example is available in the file examples/index.php., (*5)
Note 1: use 192.168.100.10 instead of 127.0.0.1 if you using Virtual Machine for the API server.
Note 2: a ConnectionException can be thrown if there is a problem with the server (timeout or connection refused, for example). Be sure to handle this case by surrounding with a try/catch and set the timeout in the Client::__construct() when an API client timeout., (*6)
``` php // examples/index.php, (*7)
<?php, (*8)
require DIR . '/../vendor/autoload.php';, (*9)
// Good response, no error, with exception $client = new \EloGank\ApiClient\Client('127.0.0.1', 8080, 'json'); $response = $client->send('EUW', 'summoner.summoner_existence', ['Foobar']);, (*10)
var_dump($response);, (*11)
// Error response, with exception $client = null; $client = new \EloGank\ApiClient\Client('127.0.0.1', 8080, 'json');, (*12)
try { $response = $client->send('EUW', 'summoner.summoner_existence', ['Not_found_summoner']); } catch (\EloGank\ApiClient\Exception\ApiException $e) { var_dump($e->getCause(), $e->getMessage()); }, (*13)
// Good response, without exception $client = null; $client = new \EloGank\ApiClient\Client('127.0.0.1', 8080, 'json', false); $response = $client->send('EUW', 'summoner.summoner_existence', ['Foobar']);, (*14)
if ($response['success']) { var_dump($response['result']); } else { // catch error }, (*15)
// Error response, without exception // Good response, without exception $client = null; $client = new \EloGank\ApiClient\Client('127.0.0.1', 8080, 'json', false); $response = $client->send('EUW', 'summoner.summoner_existence', ['Not_found_summoner']);, (*16)
if ($response['success']) { // do some process } else { var_dump($response['error']); }, (*17)
// Request with timeout $client = null; $client = new \EloGank\ApiClient\Client('128.0.0.1', 8080, 'json', false, 1); $response = $client->send('EUW', 'summoner.summoner_existence', ['Not_found_summoner']);, (*18)
if ($response['success']) { // do some process } else { var_dump($response['error']); } ```, (*19)