Advertisement
Moorer

[PHP] LoL Mobile Chat Login/Retrieve player info etc.

Jun 18th, 2016
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.56 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Class leagueChat
  4.  * Login to your LoL account via the mobile chat for IOS/Android to pull stats and such without any limits vs the limited API key Riot gives you.
  5.  * Example usage @ : https://gist.github.com/moorer2k/562199c183222b201f09ffb402ca6b65#comments
  6.  */
  7. class leagueChat {
  8.     protected $username;
  9.     protected $password;
  10.     protected $region;
  11.     protected $lang;
  12.     protected $authKey;
  13.     /**
  14.      * leagueChat constructor.
  15.      * @param $username
  16.      * @param $password
  17.      * @param string $region
  18.      * @param string $lang
  19.      */
  20.     public function __construct($username, $password, $region = 'NA1', $lang = 'en_US')
  21.     {
  22.         $this->username = $username;
  23.         $this->password = $password;
  24.         $this->region = $region;
  25.         $this->lang = $lang;
  26.         $this->Login($username, $password, $region, $lang);
  27.     }
  28.     /**
  29.      * Get summoner by ID.
  30.      *
  31.      * @param integer $id
  32.      * @return string
  33.      */
  34.     public function getSummonerById($id){
  35.         return $this->getData('https://na.api.pvp.net/api/lol/na/v1.4/summoner/' . $id);
  36.     }
  37.     /**
  38.      * Login procedure used in the mobile LoLChat client.
  39.      *
  40.      * @param $username
  41.      * @param $password
  42.      * @param $region
  43.      * @param $lang
  44.      *
  45.      * @return bool
  46.      */
  47.     protected function Login($username, $password, $region, $lang)  {
  48.         $url = 'https://auth.riotgames.com/authz/status';
  49.         $jsonRequest = json_encode(['query' => 'redirect_uri=http://localhost/oauth2-callback&client_id=leagueconnect&response_type=code&scope=openid&ui_locales=en-US']);
  50.         // First we get the initial session data to store for our next request.
  51.         $session1 = $this->postData($url, $jsonRequest, true);
  52.         $uri = 'https://auth.riotgames.com/authz/auth';
  53.         $login = json_encode(['username' => $username, 'password' => $password, 'region' => $region, 'remember' => 'true', 'lang' => $lang]);
  54.         // Now we can send our credentials to login with.
  55.         $session2 = $this->postData($uri, $login, false, $session1);
  56.         // The auth token is required as part of the next post data, so we must parse and store it for our final request.
  57.         $authToken = explode('"', explode('code=', $session2)[1])[0];
  58.         $uri2 = 'https://auth.riotgames.com/token';
  59.         //  The post paramter (at the end of $pData) client_secret is generated for each users device when installed on the mobile device. So far this has always worked for any user/pass!
  60.         $pData = 'grant_type=authorization_code&code=' . trim($authToken) . '&redirect_uri=http%3A%2F%2Flocalhost%2Foauth2-callback&client_id=leagueconnect&client_secret=amVYw7iK_qSaGUNqxRvzgs16EMgdEUdu1mDVdMNJDC4';
  61.         // Fire off the final request and check if access_token has been set!
  62.         $jRequest = $this->postData($uri2, $pData, false, '', true);
  63.         $this->authKey = trim(json_decode($jRequest)->access_token);
  64.         if(!empty($this->authKey)){
  65.             return true;
  66.         }
  67.         return false;
  68.     }
  69.     /**
  70.      * Send a GET request with the AuthKey provided from successfully authenticating.
  71.      *
  72.      * @param $url
  73.      * @return string
  74.      */
  75.     protected function getData($uri){
  76.         $response = \Httpful\Request::get($uri)
  77.             ->addHeader('Authorization', 'Bearer ' . $this->authKey)
  78.             ->send();
  79.         return $response->body;
  80.     }
  81.     /**
  82.      * Send specific POST requests as per required.
  83.      *
  84.      * @param $uri
  85.      * @param $postData
  86.      * @param bool $returnCookie
  87.      * @param string $cookieSession
  88.      * @param bool $lastRequest
  89.      * @return \Httpful\Response
  90.      */
  91.     protected function postData($uri, $postData, $returnCookie = false, $cookieSession = '', $lastRequest = false) {
  92.         if(!empty($cookieSession)){
  93.             $response = \Httpful\Request::post($uri)
  94.                 ->body($postData)
  95.                 ->addHeader('Referer', 'https://auth.riotgames.com/authorize?redirect_uri=http://localhost/oauth2-callback&client_id=leagueconnect&response_type=code&scope=openid&ui_locales=en-US')
  96.                 ->addHeader('Cookie', $cookieSession)
  97.                 ->addHeader('Content-Type', 'application/json')
  98.                 ->send();
  99.         } elseif($lastRequest) {
  100.             $response = \Httpful\Request::post($uri)
  101.                 ->body($postData)
  102.                 ->addHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')
  103.                 ->addHeader('Accept', '*')
  104.                 ->addHeader('User-Agent', 'LoL%20Friends/490 CFNetwork/758.0.2 Darwin/15.0.0')
  105.                 ->send();
  106.         } else {
  107.             $response = \Httpful\Request::post($uri)
  108.                 ->body($postData)
  109.                 ->addHeader('Referer', 'https://auth.riotgames.com/authorize?redirect_uri=http://localhost/oauth2-callback&client_id=leagueconnect&response_type=code&scope=openid&ui_locales=en-US')
  110.                 ->sendsJson()
  111.                 ->send();
  112.         }
  113.         if($returnCookie){
  114.             $respHeaders = $response->headers->toArray() ;
  115.             return explode(';', $respHeaders['set-cookie'])[0];
  116.         }
  117.         return $response;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement