forked from hanlin-studio/CFRatingColor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
78 lines (74 loc) · 2.55 KB
/
index.php
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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 7 |
// +----------------------------------------------------------------------+
// | Authors: abc1763613206 <abc1763613206@163.com> |
// +----------------------------------------------------------------------+
//
// $Id:$
$user = $_GET["user"];
$st = $_GET["style"];
$badgehd = "Location:https://img.shields.io/badge/";
set_time_limit(600);
$mainUrl = "http://codeforces.com/api/user.info?handles=*";
$ratingr = getData(str_replace("*", $user, $mainUrl)); //获取json数据并转换为数组
$styleraw = "?longCache=true&style=*";
if (isset($_GET["style"])) //是否使用自定义style
{
$style = str_replace("*", $st, $styleraw); //替换
} else {
$style = "?longCache=true&style=for-the-badge"; //默认是for-the-badge
}
function getData($url) {
$headers = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
return $result;
}
$ratstr = $ratingr["result"][0]["rating"]; //从数组中提取字符串rating
$rating = intval($ratstr); //转换为数字
if ($rating >= 2900) { //开始根据Rating判断段位
$name = "-Legendary Grandmaster ";
$color = "-red.svg";
} elseif ($rating >= 2600) {
$name = "-International Grandmaster ";
$color = "-red.svg";
} elseif ($rating >= 2400) {
$name = "-Grandmaster ";
$color = "-red.svg";
} elseif ($rating >= 2300) {
$name = "-International master ";
$color = "-ff8c00.svg";
} elseif ($rating >= 2200) {
$name = "-Master ";
$color = "-ff8c00.svg";
} elseif ($rating >= 1900) {
$name = "-Candidate Master ";
$color = "-aa00aa.svg";
} elseif ($rating >= 1600) {
$name = "-Expert ";
$color = "-blue.svg";
} elseif ($rating >= 1400) {
$name = "-Specialist ";
$color = "-03a89e.svg";
} elseif ($rating >= 1200) {
$name = "-Pupil ";
$color = "-green.svg";
} elseif ($rating > 0) {
$name = "-Newbie ";
$color = "-808080.svg";
} else {
$name = "-Unrated ";
$color = "-black.svg";
}
header($badgehd . $user . $name . $ratstr . $color . $style); //拼接并输出
?>