-
Notifications
You must be signed in to change notification settings - Fork 0
/
gojapi.php
109 lines (96 loc) · 2.3 KB
/
gojapi.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
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
108
109
<?php
/**
* Goj Judger API
* @author Rex Lee <[email protected]>
* @version 0.0.1
*/
class Judger{
private $url = "";
private $host = "";
private $port = 1005;
private $password = "";
private $login = false;
function __construct($host, $port, $password){
$this->host = $host;
$this->port = $port;
$this->password = $password;
$this->url = "http://".$this->host.":".$this->port;
$this->login = $this->login($this->password);
}
/**
* http post method
* @param string $post_string request body
* @return string response
*/
private function post($post_string){
$remote_server = $this->url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Goj Client API");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* login
* @param string $password password
* @return bool login result
*/
private function login($password){
$requ = json_encode(array('action' => "login", 'password' => $password));
$resp = $this->post($requ);
// echo $resp;
$resp_obj = json_decode($resp);
if ($resp_obj->result == true) {
$this->sid = $resp_obj->sid;
return true;
}else{
return false;
}
}
/**
* add task
* @param int $id id
* @param string $language language
* @param string $code code
* @param string $type task type
* @param array $io_data io test data
*/
public function add_task($id, $language, $code, $type, $io_data){
if (!$this->login) {
return null;
}
$requ = json_encode(array(
'action' => "task_add",
'password' => $this->password,
'sid' => $this->sid,
'time' => time(),
'language' => $language,
'code' => htmlspecialchars($code),
'type' => $type,
'io_data' => $io_data,
));
$resp = $this->post($requ);
return json_decode($resp);
}
/**
* get task status
* @param int $id task id
* @return object response info object
*/
public function get_status($id){
if (!$this->login) {
return null;
}
$requ = json_encode(array(
'action' => "task_info",
'password' => $this->password,
'sid' => $this->sid,
'id' => $id,
));
$resp = $this->post($requ);
return json_decode($resp);
}
}