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.
70 lines
1.7 KiB
70 lines
1.7 KiB
8 months ago
|
<?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 $triedEUNE = false;
|
||
|
|
||
|
public function __construct($endpoint) {
|
||
|
$this->endpoint = $endpoint;
|
||
|
}
|
||
|
|
||
|
public function useRouting(): void {
|
||
|
$this->host = "https://europe.api.riotgames.com";
|
||
|
}
|
||
|
|
||
|
public function setHeaders($headers): void {
|
||
|
$this->headers = $headers;
|
||
|
}
|
||
|
|
||
|
public function setQueries($queries): void {
|
||
|
$this->queries = $queries;
|
||
|
}
|
||
|
|
||
|
protected function getHeaderString(): string {
|
||
|
$api_key = getenv("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() {
|
||
|
$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->triedEUNE) {
|
||
|
$this->host = "https://eun1.api.riotgames.com";
|
||
|
$this->triedEUNE = true;
|
||
|
return $this->run();
|
||
|
}
|
||
|
|
||
|
return json_decode($result);
|
||
|
}
|
||
|
|
||
|
}
|