1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107:
<?php
/**
* A class to access the osu! API. It queries the API at {@link API_URL}.
*
* @author Lemmmy
*/
class OsuAPI
{
/*
* The osu! API url
*/
const API_URL = "https://osu.ppy.sh/api/";
/**
* Your private osu!API key
*
* @var string
*/
private $apiKey;
/**
* The memcache object
*
* @var Memcached
*/
private $mc;
/**
* Creates a new instance of OsuAPI
*
* @param string $apiKey Your private osu!API key
*/
public function __construct($apiKey)
{
$this->apiKey = $apiKey;
$this->mc = Utils::getMemcache();
}
/**
* Gets user information for a specific game mode
*
* @param string $username The player's username
* @param string [$mode] The game mode
*
* @return array|bool
*/
public function getUserForMode($username, $mode = "osu")
{
if ($mode < 4) {
$user = $this->mc->get("osusigv3_user_" . $mode . "_" . strtolower($username));
if (!$user) {
$request = $this->request('get_user', ['u' => $username, 'm' => $mode]);
if (isset($request) && isset($request[0])) {
$this->mc->set("osusigv3_user_" . $mode . "_" . strtolower($username), $request[0], 180);
return $request[0];
}
} else {
return $user;
}
return false;
} else {
$errorImage = new ErrorImage();
$errorImage->generate("Invalid mode", "You specified an invalid mode\nfor the signature.");
return false;
}
return false;
}
/**
* Request from the osu!API
*
* @param string $url The resource to fetch
* @param array $params A list of arguments to give for the resource
*
* @return array|null The decoded JSON object containing the fetched resource, or null.
*/
public function request($url, $params = [])
{
$params = array_merge(["k" => $this->apiKey], $params);
$url = static::API_URL . $url . '?' . http_build_query($params);
return $this->decode(file_get_contents($url));
}
/**
* Decode a response from the API
*
* @param string $content The response from the API
* @return array|null The decoded JSON object, or null.
*/
protected function decode($content)
{
if (strlen($content) > 0 && $content) {
return json_decode($content, true);
}
return null;
}
}