Riot API Tutorial (PHP)

This post got deleted from the Riot developer site forums for some reason so I’m re-uploading it here.

Hello everyone, I’m a WordPress web developer / owner of the site LoLSpectator.tv. I wanted to share some info on how I’ve implemented the API in PHP since it took me a stupidly long time to figure out and I couldn’t find any in-depth guides on how to do this.

In this guide I’ll show you how to convert a summoner’s name into an ID:

• Create a new function. We’ll need 2 input pieces of data for this to work – the first obviously being the name of the summoner, and the second being the server they are on. Check out the list of regional endpoints here.

function summoner_name($summoner, $server) {
}

• Before we run any Curl functions we need to do a few things to properly encode the summoner’s name. In this step we will perform a raw URL encode, then convert all characters to lowercase. This will make sure that the string is prepared to be interpreted by Riot’s API.

function summoner_name($summoner, $server) {
$summoner_encoded = rawurlencode($summoner);
$summoner_lower = strtolower($summoner_enc);
}

• Add the necessary Curl code along with the url for the “summoner by name” function of the API, execute it, and return the result. Insert your personal API key in place of <key>.

function summoner_name($summoner, $server) {
$summoner_encoded = rawurlencode($summoner);
$summoner_lower = strtolower($summoner_enc);
$curl = curl_init(‘https://’ . $server . ‘.api.pvp.net/api/lol/’ . $server . ‘/v1.4/summoner/by-name/’ . $summoner . ‘?api_key=<key>’);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}

• This is about it for the function, you can add some error code handling if you want. The request will return an array in $curl. We now need to set the $summoner and $server variables, execute the function, and decode the array that is returned so we can pull out the value that we want.

$summoner = ‘RiotSchmick’;
$server = ‘na’;
$summoner_info = summoner_name($summoner, $server);
$summoner_info_array = json_decode($summoner_info, true);

• The array is now stored in the variable $summoner_info_array, so now we can pull individual values out of it. You can view the array by using print_r($summoner_info_array); if you want. Right now if you were to use this function it should look like this:

Array
(
[riotschmick] => Array
(
[id] => 585897
[name] => RiotSchmick
[profileIconId] => 546
[summonerLevel] => 30
[revisionDate] => 1427175557000
)

)

• Lets try to pull the summoner’s ID from the array. The variable for this would currently look like this: $summoner_info_array[‘riotschmick’][‘id’] but since ‘riotschmick’ will change when a different summoner’s info is called, we need to compute this value with another function. We need to convert the name to lowercase characters and remove the spaces. It should look something like this:

function summoner_info_array_name($summoner) {
$summoner_lower = mb_strtolower($summoner, ‘UTF-8’);
$summoner_nospaces = str_replace(‘ ‘, ”, $summoner_lower);
return $summoner_nospaces;
}

• All that’s left to do now is to pull the desired value from the array. The final code should look like this:

$summoner = ‘RiotSchmick’;
$server = ‘na’;
$summoner_info = summoner_name($summoner, $server);
$summoner_info_array = json_decode($summoner_info, true);
$summoner_info_array_name = summoner_info_array_name($summoner);
$summoner_id = $summoner_info_array[$summoner_info_array_name][‘id’];

• There you have it! With this, you’ll be able to display the summoner’s ID or use it in another API call with the $summoner_id variable. This whole process can be tweaked to use pretty much any part of the API. Let me know if you have any questions/comments!