LoginSignup
5
6

More than 3 years have passed since last update.

RIOT APIで無料チャンプ表示とゲーム情報取得など

Last updated at Posted at 2019-05-08

この記事で紹介する範囲

・無料チャンプの表示
・試合の情報を抜き出す
・名前を変更しているプレイヤーが昔どのような名前を使っていたのか調べる

環境

php 5.6.40

無料チャンプの表示

2018年6月30日からlol-static-data-v3が非推奨というか削除されました
これによってDDragonからデータ取得をする手間が増えました
以下のサイトで紹介されています
https://www.utakata.work/entry/20180811/1533996560

なのでCHAMPION-V3で無料チャンプのIDを取得し、DDragonで紐づけることが必要になります

//自分のIDで取得したAPIkeyを入れてください
$key="*********************************";
//DDragonからチャンプの名前を取得
$json= json_decode(file_get_contents('http://ddragon.leagueoflegends.com/cdn/6.24.1/data/ja_JP/champion.json'), true);
//無料チャンプのID取得
$free=json_decode(file_get_contents('https://jp1.api.riotgames.com/lol/platform/v3/champion-rotations'.'?api_key='.$key), true);
//無料チャンプのIDと名前を紐づけ
$freechamp=$free['freeChampionIds'];
foreach($json['data'] as $key => $value){
  foreach($freechamp as $value){
       if($json['data'][$key]['key']==$value){
//画像データもDDragonにあります
         print '<image src="http://ddragon.leagueoflegends.com/cdn/6.24.1/img/champion/'.$key.'.png" width="50" height="50"></image>';
       };
     }
}
?>

実行結果は以下(2019/05/06)
無題.png

試合の情報を抜き出す

抜き出すといっても色々あると思うのですが、ここでは何時プレイしたのか、勝敗、ロール、使用チャンプを出すことにします
あとほかのサイトでも紹介されているプロフィールアイコン、配属グループ名、Tier、勝敗数などもついでに出します

<?php
$apikey="RGAPI-2731064c-c779-4026-8f7f-2242da54d8f7";
session_start();
if(isset($_POST['submit'])){
  $_SESSION['NAME']= $_POST['username'];
}
if(!preg_match('/exist/',$_SESSION['joutai'])){
  $json = json_decode(file_get_contents('https://jp1.api.riotgames.com/lol/summoner/v4/summoners/by-name/'.$_SESSION['NAME'].'?api_key='.$apikey), true);
  //ほしい情報をjsonから引き出す
  $_SESSION['icon'] = $json["profileIconId"];
  $_SESSION['TNAME'] = $json['name'];
  $_SESSION['level'] =  $json['summonerLevel'];
  $_SESSION['id'] = $json['id'];
  $_SESSION['accountId']=$json['accountId'];

  //ランクの情報 tier、グループ、LP、勝敗数、divison、
  $json = json_decode(file_get_contents('https://jp1.api.riotgames.com/lol/league/v4/positions/by-summoner/' . $_SESSION['id'] . '?api_key='.$apikey), true);
  $_SESSION['LP']=$json[0]['leaguePoints'];
  $_SESSION['division']=$json[0]['rank'];
  $_SESSION['losses']=$json[0]['losses'];
  $_SESSION['wins']=$json[0]['wins'];
  $_SESSION['tier']=$json[0]['tier'];
  $_SESSION['name1'] = $json[0]['leagueName'];//テーモのつけた名前
  $_SESSION['tierD'] = $json[0]['tier'];
}

 ?>
<?php   print '<image src="http://ddragon.leagueoflegends.com/cdn/6.24.1/img/profileicon/'.$_SESSION['icon'].'.png" width="100px"></image>'; ?>

                       <p><span style="font-weight : bold"><?php echo $_SESSION['TNAME']; ?></span></p>
サモナーレベル:<?php echo $_SESSION['level']; ?>

                    </li>
                </ul>
                <ul class="products">
                    <li>
<?php
    if(!empty($_SESSION['tierD'])){
            print '<image src="../img/'.$_SESSION['tier'].$_SESSION['division'].'.png"width="100px"></image>';
            print $_SESSION['tierD'].$_SESSION['division'].'<br>';
            print $_SESSION['LP'].'LP';
            print '<font color="#999999">  /  </font>';
            print '<font color="#999999">'.$_SESSION['wins'].'勝</font>';
            print '<font color="#999999">'.$_SESSION['losses'].'敗</font><br>';
              $syouritu = number_format($_SESSION['wins']/($_SESSION['wins']+$_SESSION['losses']),2)*100;
            print '<font color="#999999">勝率'.$syouritu.'%</font><br>';
            print '<font color="#999999">'.$_SESSION['name1'].'</font>';

                 }else{
            print '<image src="../img/unrank.png"width="100px"></image>';
            print '<br>';
            print '<br>';
              echo 'Unrank';
    }
?>
                    </li>
                  </ul>
            </div>
<?php
    $aa=1;
  //timestanp
    $json = json_decode(file_get_contents('https://jp1.api.riotgames.com/lol/match/v4/matchlists/by-account/' . $_SESSION['accountId'] . '?api_key='.$apikey), true);

    $json1= json_decode(file_get_contents('http://ddragon.leagueoflegends.com/cdn/6.24.1/data/ja_JP/champion.json'), true);
    $test=$json['matches'];
    foreach($test as $key => $value){
      if($value['queue']=='420'){
        $syori = "1";
      }else{
        $syori="0";
      }
      if($syori=="1"){
            date_default_timezone_set('Asia/Tokyo');
            $timestamp = time();
            $tmp = substr($value['timestamp'],0,10);

            echo "プレイ日時:";
            echo date('Y年m月d日H時i分s秒',$tmp);
            echo '<br>';
            echo "Role:";
            echo $value["lane"];
            echo '<br>';
            echo "使用チャンプ:";
            foreach($json1['data'] as $key1 => $value1){
              if($json1['data'][$key1]['key']==$value["champion"]){
                echo $json1['data'][$key1]['name'];
              }
            }
            echo '<br>';
            echo "matchID:";
            echo $value["gameId"];
            echo '<br>';
            $participantId="";
            $json2 = json_decode(file_get_contents('https://jp1.api.riotgames.com/lol/match/v4/matches/'. $value["gameId"] .'?api_key='.$apikey), true);
            foreach($json2['participantIdentities'] as $key2 => $value2){
              if($json2['participantIdentities'][$key2]['player']['summonerName'] == $_SESSION['NAME']){
                $participantId = $json2['participantIdentities'][$key2]['participantId'];
              }
            }
            if($participantId<=5){
               if($json2['teams'][0]['win']=="Win"){
                 echo "勝利";
                 echo "<br>";
               }else{
                 echo "敗北";
                 echo "<br>";
               }
            }else{
              if($json2['teams'][1]['win']=="win"){
                echo "勝利";
                echo "<br>";
              }else{
                echo "敗北";
                echo "<br>";
              }
            }
            echo "<hr>";
            if($aa==5){
              break;
            }
          $aa++;
        }
    }
 ?>

実行結果
無題.png
サモナーは配信で有名なスタンミジャパンさんにしてみましたが、無事取れてるはず
試合がどういった風に行われていたかを知りたい場合は
/lol/match/v4/timelines/by-match/{matchId}
上記を叩けばでるのでやってみてください

名前を変更しているプレイヤーが昔どのような名前を使っていたのか調べる

需要があれば

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6