-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathGTBaseApi.php
76 lines (66 loc) · 2.28 KB
/
GTBaseApi.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
<?php
class GTBaseApi
{
protected $gtClient;
protected function post($api, $params)
{
return $this->httpRequest($api, $params, GTHttpManager::HTTP_METHOD_POST);
}
protected function put($api, $params)
{
return $this->httpRequest($api, $params, GTHttpManager::HTTP_METHOD_PUT);
}
protected function get($api, $params)
{
return $this->httpRequest($api, $params, GTHttpManager::HTTP_METHOD_GET);
}
protected function delete($api, $params)
{
return $this->httpRequest($api, $params, GTHttpManager::HTTP_METHOD_DELETE);
}
private function httpRequest($api, $params, $method, $gzip = false)
{
try {
$rep = GTHttpManager::httpRequest($this->getUrl($api), $params, $this->buildHead(), $gzip, $method);
} catch (GTException $e) {
throw $e;
}
if ($rep != null) {
if ('10001' == $rep['code']) {
try {
if ($this->gtClient->auth()) {
$rep = GTHttpManager::httpRequest($this->getUrl($api), $params, $this->buildHead(), $gzip, $method);
}
} catch (GTException $e) {
throw $e;
}
} else if ('301' == $rep['code']) {
if (empty($rep["data"]) || empty($rep["data"]["host_list"]) || empty($rep["data"]["host_list"][0]["domain_list"])) {
throw new GTException("域名错误");
}
$this->gtClient->setDomainUrlList($rep["data"]["host_list"][0]["domain_list"]);
try {
$rep = GTHttpManager::httpRequest($this->getUrl($api), $params, $this->buildHead(), $gzip, $method);
} catch (GTException $e) {
throw $e;
}
}
}
return $rep;
}
private function analysisUrlList()
{
}
private function buildHead()
{
$headers = array();
if ($this->gtClient->getAuthToken() != null) {
array_push($headers, "token:" . $this->gtClient->getAuthToken());
}
return $headers;
}
private function getUrl($api)
{
return $this->gtClient->getHost() . "/v2/" . $this->gtClient->getAppId() . $api;
}
}