You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

81 lines
2.0 KiB

<?php
class RiotRequest {
protected string $endpoint;
protected array $headers = array();
protected array $queries = array();
protected string $host = "https://euw1.api.riotgames.com";
public array $responseHeader = array();
public int $responseCode;
private bool $isEUNE = false;
private bool $usesRouting = false;
public function __construct($endpoint, $isEUNE=false) {
$this->endpoint = $endpoint;
if ($isEUNE){
$this->switchToEUNE();
}
}
public function useRouting(): void {
$this->host = "https://europe.api.riotgames.com";
$this->usesRouting = true;
}
public function setHeaders($headers): void {
$this->headers = $headers;
}
public function setQueries($queries): void {
$this->queries = $queries;
}
protected function getHeaderString(): string {
$api_key = $_ENV["RIOT_API_KEY"];
$header = "X-Riot-Token: $api_key\r\n";
foreach ($this->headers as $key => $value) {
$header .= "$key: $value\r\n";
}
return $header;
}
private function getFinalURL(): string {
$queryString = "?";
foreach ($this->queries as $key => $value) {
$queryString .= "$key=$value&";
}
$url = $this->host . "/" . $this->endpoint . $queryString;
return str_replace(" ", "", $url);
}
public function run(&$successWithEUNE=false) {
$opts = [
"http" => [
"method" => "GET",
"header" => $this->getHeaderString()
]
];
$context = stream_context_create($opts);
$url = $this->getFinalURL();
$result = @file_get_contents($url, false, $context);
$this->responseHeader = $http_response_header;
preg_match_all("/HTTP\/\d\.\d\s+(\d+)\s+/", $this->responseHeader[0], $matches);
$this->responseCode = intval($matches[1][0]);
if ($this->responseCode == 404 && !$this->isEUNE && !$this->usesRouting) {
$this->switchToEUNE();
return $this->run($successWithEUNE);
}
$successWithEUNE = $this->isEUNE;
return json_decode($result);
}
private function switchToEUNE(): void {
$this->host = "https://eun1.api.riotgames.com";
$this->isEUNE = true;
}
}