Moorer icon

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

Moorer | PRO | 06/18/16 04:17:21 PM UTC | 0 ⭐ | 991 👁️ | Never ⏰ | []
PHP |

4.56 KB

|

None

|

0 👍

/

0 👎

<?php
/**
 * Class leagueChat
 * 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. 
 * Example usage @ : https://gist.github.com/moorer2k/562199c183222b201f09ffb402ca6b65#comments
 */
class leagueChat {
    protected $username;
    protected $password;
    protected $region;
    protected $lang;
    protected $authKey;
    /**
     * leagueChat constructor.
     * @param $username
     * @param $password
     * @param string $region
     * @param string $lang
     */
    public function __construct($username, $password, $region = 'NA1', $lang = 'en_US')
    {
        $this->username = $username;
        $this->password = $password;
        $this->region = $region;
        $this->lang = $lang;
        $this->Login($username, $password, $region, $lang);
    }
    /**
     * Get summoner by ID.
     * 
     * @param integer $id
     * @return string
     */
    public function getSummonerById($id){
        return $this->getData('https://na.api.pvp.net/api/lol/na/v1.4/summoner/' . $id);
    }
    /**
     * Login procedure used in the mobile LoLChat client.
     *
     * @param $username
     * @param $password
     * @param $region
     * @param $lang
     *
     * @return bool
     */
    protected function Login($username, $password, $region, $lang)  {
        $url = 'https://auth.riotgames.com/authz/status';
        $jsonRequest = json_encode(['query' => 'redirect_uri=http://localhost/oauth2-callback&client_id=leagueconnect&response_type=code&scope=openid&ui_locales=en-US']);
        // First we get the initial session data to store for our next request.
        $session1 = $this->postData($url, $jsonRequest, true);
        $uri = 'https://auth.riotgames.com/authz/auth';
        $login = json_encode(['username' => $username, 'password' => $password, 'region' => $region, 'remember' => 'true', 'lang' => $lang]);
        // Now we can send our credentials to login with.
        $session2 = $this->postData($uri, $login, false, $session1);
        // The auth token is required as part of the next post data, so we must parse and store it for our final request.
        $authToken = explode('"', explode('code=', $session2)[1])[0];
        $uri2 = 'https://auth.riotgames.com/token';
        //  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!
        $pData = 'grant_type=authorization_code&code=' . trim($authToken) . '&redirect_uri=http%3A%2F%2Flocalhost%2Foauth2-callback&client_id=leagueconnect&client_secret=amVYw7iK_qSaGUNqxRvzgs16EMgdEUdu1mDVdMNJDC4';
        // Fire off the final request and check if access_token has been set!
        $jRequest = $this->postData($uri2, $pData, false, '', true);
        $this->authKey = trim(json_decode($jRequest)->access_token);
        if(!empty($this->authKey)){
            return true;
        }
        return false;
    }
    /**
     * Send a GET request with the AuthKey provided from successfully authenticating.
     *
     * @param $url
     * @return string
     */
    protected function getData($uri){
        $response = \Httpful\Request::get($uri)
            ->addHeader('Authorization', 'Bearer ' . $this->authKey)
            ->send();
        return $response->body;
    }
    /**
     * Send specific POST requests as per required.
     *
     * @param $uri
     * @param $postData
     * @param bool $returnCookie
     * @param string $cookieSession
     * @param bool $lastRequest
     * @return \Httpful\Response
     */
    protected function postData($uri, $postData, $returnCookie = false, $cookieSession = '', $lastRequest = false) {
        if(!empty($cookieSession)){
            $response = \Httpful\Request::post($uri)
                ->body($postData)
                ->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')
                ->addHeader('Cookie', $cookieSession)
                ->addHeader('Content-Type', 'application/json')
                ->send();
        } elseif($lastRequest) {
            $response = \Httpful\Request::post($uri)
                ->body($postData)
                ->addHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')
                ->addHeader('Accept', '*')
                ->addHeader('User-Agent', 'LoL%20Friends/490 CFNetwork/758.0.2 Darwin/15.0.0')
                ->send();
        } else {
            $response = \Httpful\Request::post($uri)
                ->body($postData)
                ->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')
                ->sendsJson()
                ->send();
        }
        if($returnCookie){
            $respHeaders = $response->headers->toArray() ;
            return explode(';', $respHeaders['set-cookie'])[0];
        }
        return $response;
    }
}

Comments