@@ -7,41 +7,41 @@ |
||
7 | 7 | */ |
8 | 8 | class TopSecretRegisterRequest |
9 | 9 | { |
10 | - /** |
|
11 | - * 用户id,保证唯一 |
|
12 | - **/ |
|
13 | - private $userId; |
|
10 | + /** |
|
11 | + * 用户id,保证唯一 |
|
12 | + **/ |
|
13 | + private $userId; |
|
14 | 14 | |
15 | - private $apiParas = array(); |
|
15 | + private $apiParas = array(); |
|
16 | 16 | |
17 | - public function setUserId($userId) |
|
18 | - { |
|
19 | - $this->userId = $userId; |
|
20 | - $this->apiParas["user_id"] = $userId; |
|
21 | - } |
|
17 | + public function setUserId($userId) |
|
18 | + { |
|
19 | + $this->userId = $userId; |
|
20 | + $this->apiParas["user_id"] = $userId; |
|
21 | + } |
|
22 | 22 | |
23 | - public function getUserId() |
|
24 | - { |
|
25 | - return $this->userId; |
|
26 | - } |
|
23 | + public function getUserId() |
|
24 | + { |
|
25 | + return $this->userId; |
|
26 | + } |
|
27 | 27 | |
28 | - public function getApiMethodName() |
|
29 | - { |
|
30 | - return "taobao.top.secret.register"; |
|
31 | - } |
|
28 | + public function getApiMethodName() |
|
29 | + { |
|
30 | + return "taobao.top.secret.register"; |
|
31 | + } |
|
32 | 32 | |
33 | - public function getApiParas() |
|
34 | - { |
|
35 | - return $this->apiParas; |
|
36 | - } |
|
33 | + public function getApiParas() |
|
34 | + { |
|
35 | + return $this->apiParas; |
|
36 | + } |
|
37 | 37 | |
38 | - public function check() |
|
39 | - { |
|
38 | + public function check() |
|
39 | + { |
|
40 | 40 | |
41 | - } |
|
41 | + } |
|
42 | 42 | |
43 | - public function putOtherTextParam($key, $value) { |
|
44 | - $this->apiParas[$key] = $value; |
|
45 | - $this->$key = $value; |
|
46 | - } |
|
43 | + public function putOtherTextParam($key, $value) { |
|
44 | + $this->apiParas[$key] = $value; |
|
45 | + $this->$key = $value; |
|
46 | + } |
|
47 | 47 | } |
@@ -7,42 +7,42 @@ |
||
7 | 7 | */ |
8 | 8 | class TopAuthTokenRefreshRequest |
9 | 9 | { |
10 | - /** |
|
11 | - * grantType==refresh_token 时需要 |
|
12 | - **/ |
|
13 | - private $refreshToken; |
|
10 | + /** |
|
11 | + * grantType==refresh_token 时需要 |
|
12 | + **/ |
|
13 | + private $refreshToken; |
|
14 | 14 | |
15 | - private $apiParas = array(); |
|
15 | + private $apiParas = array(); |
|
16 | 16 | |
17 | - public function setRefreshToken($refreshToken) |
|
18 | - { |
|
19 | - $this->refreshToken = $refreshToken; |
|
20 | - $this->apiParas["refresh_token"] = $refreshToken; |
|
21 | - } |
|
17 | + public function setRefreshToken($refreshToken) |
|
18 | + { |
|
19 | + $this->refreshToken = $refreshToken; |
|
20 | + $this->apiParas["refresh_token"] = $refreshToken; |
|
21 | + } |
|
22 | 22 | |
23 | - public function getRefreshToken() |
|
24 | - { |
|
25 | - return $this->refreshToken; |
|
26 | - } |
|
23 | + public function getRefreshToken() |
|
24 | + { |
|
25 | + return $this->refreshToken; |
|
26 | + } |
|
27 | 27 | |
28 | - public function getApiMethodName() |
|
29 | - { |
|
30 | - return "taobao.top.auth.token.refresh"; |
|
31 | - } |
|
28 | + public function getApiMethodName() |
|
29 | + { |
|
30 | + return "taobao.top.auth.token.refresh"; |
|
31 | + } |
|
32 | 32 | |
33 | - public function getApiParas() |
|
34 | - { |
|
35 | - return $this->apiParas; |
|
36 | - } |
|
33 | + public function getApiParas() |
|
34 | + { |
|
35 | + return $this->apiParas; |
|
36 | + } |
|
37 | 37 | |
38 | - public function check() |
|
39 | - { |
|
38 | + public function check() |
|
39 | + { |
|
40 | 40 | |
41 | - RequestCheckUtil::checkNotNull($this->refreshToken,"refreshToken"); |
|
42 | - } |
|
41 | + RequestCheckUtil::checkNotNull($this->refreshToken,"refreshToken"); |
|
42 | + } |
|
43 | 43 | |
44 | - public function putOtherTextParam($key, $value) { |
|
45 | - $this->apiParas[$key] = $value; |
|
46 | - $this->$key = $value; |
|
47 | - } |
|
44 | + public function putOtherTextParam($key, $value) { |
|
45 | + $this->apiParas[$key] = $value; |
|
46 | + $this->$key = $value; |
|
47 | + } |
|
48 | 48 | } |
@@ -38,7 +38,7 @@ |
||
38 | 38 | public function check() |
39 | 39 | { |
40 | 40 | |
41 | - RequestCheckUtil::checkNotNull($this->refreshToken,"refreshToken"); |
|
41 | + RequestCheckUtil::checkNotNull($this->refreshToken, "refreshToken"); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | public function putOtherTextParam($key, $value) { |
@@ -8,25 +8,25 @@ |
||
8 | 8 | class OpenuidGetRequest |
9 | 9 | { |
10 | 10 | |
11 | - private $apiParas = array(); |
|
11 | + private $apiParas = array(); |
|
12 | 12 | |
13 | - public function getApiMethodName() |
|
14 | - { |
|
15 | - return "taobao.openuid.get"; |
|
16 | - } |
|
13 | + public function getApiMethodName() |
|
14 | + { |
|
15 | + return "taobao.openuid.get"; |
|
16 | + } |
|
17 | 17 | |
18 | - public function getApiParas() |
|
19 | - { |
|
20 | - return $this->apiParas; |
|
21 | - } |
|
18 | + public function getApiParas() |
|
19 | + { |
|
20 | + return $this->apiParas; |
|
21 | + } |
|
22 | 22 | |
23 | - public function check() |
|
24 | - { |
|
23 | + public function check() |
|
24 | + { |
|
25 | 25 | |
26 | - } |
|
26 | + } |
|
27 | 27 | |
28 | - public function putOtherTextParam($key, $value) { |
|
29 | - $this->apiParas[$key] = $value; |
|
30 | - $this->$key = $value; |
|
31 | - } |
|
28 | + public function putOtherTextParam($key, $value) { |
|
29 | + $this->apiParas[$key] = $value; |
|
30 | + $this->$key = $value; |
|
31 | + } |
|
32 | 32 | } |
@@ -1,199 +1,199 @@ |
||
1 | 1 | <?php |
2 | 2 | class ClusterTopClient extends TopClient { |
3 | 3 | |
4 | - private static $dnsconfig; |
|
5 | - private static $syncDate = 0; |
|
6 | - private static $applicationVar ; |
|
7 | - private static $cfgDuration = 10; |
|
8 | - |
|
9 | - public function __construct($appkey = "",$secretKey = ""){ |
|
10 | - ClusterTopClient::$applicationVar = new ApplicationVar; |
|
11 | - $this->appkey = $appkey; |
|
12 | - $this->secretKey = $secretKey ; |
|
13 | - $saveConfig = ClusterTopClient::$applicationVar->getValue(); |
|
14 | - |
|
15 | - if($saveConfig){ |
|
16 | - $tmpConfig = $saveConfig['dnsconfig']; |
|
17 | - ClusterTopClient::$dnsconfig = $this->object_to_array($tmpConfig); |
|
18 | - unset($tmpConfig); |
|
19 | - |
|
20 | - ClusterTopClient::$syncDate = $saveConfig['syncDate']; |
|
21 | - if(!ClusterTopClient::$syncDate) |
|
22 | - ClusterTopClient::$syncDate = 0; |
|
23 | - } |
|
24 | - } |
|
25 | - |
|
26 | - public function __destruct(){ |
|
27 | - if(ClusterTopClient::$dnsconfig && ClusterTopClient::$syncDate){ |
|
28 | - ClusterTopClient::$applicationVar->setValue("dnsconfig",ClusterTopClient::$dnsconfig); |
|
29 | - ClusterTopClient::$applicationVar->setValue("syncDate",ClusterTopClient::$syncDate); |
|
30 | - ClusterTopClient::$applicationVar->write(); |
|
31 | - } |
|
32 | - } |
|
33 | - |
|
34 | - public function execute($request = null, $session = null,$bestUrl = null){ |
|
35 | - $currentDate = date('U'); |
|
36 | - $syncDuration = $this->getDnsConfigSyncDuration(); |
|
37 | - $bestUrl = $this->getBestVipUrl($this->gatewayUrl,$request->getApiMethodName(),$session); |
|
38 | - if($currentDate - ClusterTopClient::$syncDate > $syncDuration * 60){ |
|
39 | - $httpdns = new HttpdnsGetRequest; |
|
40 | - ClusterTopClient::$dnsconfig = json_decode(parent::execute($httpdns,null,$bestUrl)->result,true); |
|
41 | - $syncDate = date('U'); |
|
42 | - ClusterTopClient::$syncDate = $syncDate ; |
|
43 | - } |
|
44 | - return parent::execute($request,$session,$bestUrl); |
|
45 | - } |
|
46 | - |
|
47 | - private function getDnsConfigSyncDuration(){ |
|
48 | - if(ClusterTopClient::$cfgDuration){ |
|
49 | - return ClusterTopClient::$cfgDuration; |
|
50 | - } |
|
51 | - if(!ClusterTopClient::$dnsconfig){ |
|
52 | - return ClusterTopClient::$cfgDuration; |
|
53 | - } |
|
54 | - $config = json_encode(ClusterTopClient::$dnsconfig); |
|
55 | - if(!$config){ |
|
56 | - return ClusterTopClient::$cfgDuration; |
|
57 | - } |
|
58 | - $config = ClusterTopClient::$dnsconfig['config']; |
|
59 | - $duration = $config['interval']; |
|
60 | - ClusterTopClient::$cfgDuration = $duration; |
|
61 | - |
|
62 | - return ClusterTopClient::$cfgDuration; |
|
63 | - } |
|
64 | - |
|
65 | - private function getBestVipUrl($url,$apiname = null,$session = null){ |
|
66 | - $config = ClusterTopClient::$dnsconfig['config']; |
|
67 | - $degrade = $config['degrade']; |
|
68 | - if(strcmp($degrade,'true') == 0){ |
|
69 | - return $url; |
|
70 | - } |
|
71 | - $currentEnv = $this->getEnvByApiName($apiname,$session); |
|
72 | - $vip = $this->getVipByEnv($url,$currentEnv); |
|
73 | - if($vip) |
|
74 | - return $vip; |
|
75 | - return $url; |
|
76 | - } |
|
77 | - |
|
78 | - private function getVipByEnv($comUrl,$currentEnv){ |
|
79 | - $urlSchema = parse_url($comUrl); |
|
80 | - if(!$urlSchema) |
|
81 | - return null; |
|
82 | - if(!ClusterTopClient::$dnsconfig['env']) |
|
83 | - return null; |
|
4 | + private static $dnsconfig; |
|
5 | + private static $syncDate = 0; |
|
6 | + private static $applicationVar ; |
|
7 | + private static $cfgDuration = 10; |
|
8 | + |
|
9 | + public function __construct($appkey = "",$secretKey = ""){ |
|
10 | + ClusterTopClient::$applicationVar = new ApplicationVar; |
|
11 | + $this->appkey = $appkey; |
|
12 | + $this->secretKey = $secretKey ; |
|
13 | + $saveConfig = ClusterTopClient::$applicationVar->getValue(); |
|
14 | + |
|
15 | + if($saveConfig){ |
|
16 | + $tmpConfig = $saveConfig['dnsconfig']; |
|
17 | + ClusterTopClient::$dnsconfig = $this->object_to_array($tmpConfig); |
|
18 | + unset($tmpConfig); |
|
19 | + |
|
20 | + ClusterTopClient::$syncDate = $saveConfig['syncDate']; |
|
21 | + if(!ClusterTopClient::$syncDate) |
|
22 | + ClusterTopClient::$syncDate = 0; |
|
23 | + } |
|
24 | + } |
|
25 | + |
|
26 | + public function __destruct(){ |
|
27 | + if(ClusterTopClient::$dnsconfig && ClusterTopClient::$syncDate){ |
|
28 | + ClusterTopClient::$applicationVar->setValue("dnsconfig",ClusterTopClient::$dnsconfig); |
|
29 | + ClusterTopClient::$applicationVar->setValue("syncDate",ClusterTopClient::$syncDate); |
|
30 | + ClusterTopClient::$applicationVar->write(); |
|
31 | + } |
|
32 | + } |
|
33 | + |
|
34 | + public function execute($request = null, $session = null,$bestUrl = null){ |
|
35 | + $currentDate = date('U'); |
|
36 | + $syncDuration = $this->getDnsConfigSyncDuration(); |
|
37 | + $bestUrl = $this->getBestVipUrl($this->gatewayUrl,$request->getApiMethodName(),$session); |
|
38 | + if($currentDate - ClusterTopClient::$syncDate > $syncDuration * 60){ |
|
39 | + $httpdns = new HttpdnsGetRequest; |
|
40 | + ClusterTopClient::$dnsconfig = json_decode(parent::execute($httpdns,null,$bestUrl)->result,true); |
|
41 | + $syncDate = date('U'); |
|
42 | + ClusterTopClient::$syncDate = $syncDate ; |
|
43 | + } |
|
44 | + return parent::execute($request,$session,$bestUrl); |
|
45 | + } |
|
46 | + |
|
47 | + private function getDnsConfigSyncDuration(){ |
|
48 | + if(ClusterTopClient::$cfgDuration){ |
|
49 | + return ClusterTopClient::$cfgDuration; |
|
50 | + } |
|
51 | + if(!ClusterTopClient::$dnsconfig){ |
|
52 | + return ClusterTopClient::$cfgDuration; |
|
53 | + } |
|
54 | + $config = json_encode(ClusterTopClient::$dnsconfig); |
|
55 | + if(!$config){ |
|
56 | + return ClusterTopClient::$cfgDuration; |
|
57 | + } |
|
58 | + $config = ClusterTopClient::$dnsconfig['config']; |
|
59 | + $duration = $config['interval']; |
|
60 | + ClusterTopClient::$cfgDuration = $duration; |
|
61 | + |
|
62 | + return ClusterTopClient::$cfgDuration; |
|
63 | + } |
|
64 | + |
|
65 | + private function getBestVipUrl($url,$apiname = null,$session = null){ |
|
66 | + $config = ClusterTopClient::$dnsconfig['config']; |
|
67 | + $degrade = $config['degrade']; |
|
68 | + if(strcmp($degrade,'true') == 0){ |
|
69 | + return $url; |
|
70 | + } |
|
71 | + $currentEnv = $this->getEnvByApiName($apiname,$session); |
|
72 | + $vip = $this->getVipByEnv($url,$currentEnv); |
|
73 | + if($vip) |
|
74 | + return $vip; |
|
75 | + return $url; |
|
76 | + } |
|
77 | + |
|
78 | + private function getVipByEnv($comUrl,$currentEnv){ |
|
79 | + $urlSchema = parse_url($comUrl); |
|
80 | + if(!$urlSchema) |
|
81 | + return null; |
|
82 | + if(!ClusterTopClient::$dnsconfig['env']) |
|
83 | + return null; |
|
84 | 84 | |
85 | - if(!array_key_exists($currentEnv,ClusterTopClient::$dnsconfig['env'])) |
|
86 | - return null; |
|
87 | - |
|
88 | - $hostList = ClusterTopClient::$dnsconfig['env'][$currentEnv]; |
|
89 | - if(!$hostList) |
|
90 | - return null ; |
|
91 | - |
|
92 | - $vipList = null; |
|
93 | - foreach ($hostList as $key => $value) { |
|
94 | - if(strcmp($key,$urlSchema['host']) == 0 && strcmp($value['proto'],$urlSchema['scheme']) == 0){ |
|
95 | - $vipList = $value; |
|
96 | - break; |
|
97 | - } |
|
98 | - } |
|
99 | - $vip = $this->getRandomWeightElement($vipList['vip']); |
|
85 | + if(!array_key_exists($currentEnv,ClusterTopClient::$dnsconfig['env'])) |
|
86 | + return null; |
|
87 | + |
|
88 | + $hostList = ClusterTopClient::$dnsconfig['env'][$currentEnv]; |
|
89 | + if(!$hostList) |
|
90 | + return null ; |
|
91 | + |
|
92 | + $vipList = null; |
|
93 | + foreach ($hostList as $key => $value) { |
|
94 | + if(strcmp($key,$urlSchema['host']) == 0 && strcmp($value['proto'],$urlSchema['scheme']) == 0){ |
|
95 | + $vipList = $value; |
|
96 | + break; |
|
97 | + } |
|
98 | + } |
|
99 | + $vip = $this->getRandomWeightElement($vipList['vip']); |
|
100 | 100 | |
101 | - if($vip){ |
|
102 | - return $urlSchema['scheme']."://".$vip.$urlSchema['path']; |
|
103 | - } |
|
104 | - return null; |
|
105 | - } |
|
106 | - |
|
107 | - private function getEnvByApiName($apiName,$session=""){ |
|
108 | - $apiCfgArray = ClusterTopClient::$dnsconfig['api']; |
|
109 | - if($apiCfgArray){ |
|
110 | - if(array_key_exists($apiName,$apiCfgArray)){ |
|
111 | - $apiCfg = $apiCfgArray[$apiName]; |
|
112 | - if(array_key_exists('user',$apiCfg)){ |
|
113 | - $userFlag = $apiCfg['user']; |
|
114 | - $flag = $this->getUserFlag($session); |
|
115 | - if($userFlag && $flag ){ |
|
116 | - return $this->getEnvBySessionFlag($userFlag,$flag); |
|
117 | - }else{ |
|
118 | - return $this->getRandomWeightElement($apiCfg['rule']); |
|
119 | - } |
|
120 | - } |
|
121 | - } |
|
122 | - } |
|
123 | - return $this->getDeafultEnv(); |
|
124 | - } |
|
125 | - |
|
126 | - private function getUserFlag($session){ |
|
127 | - if($session && strlen($session) > 5){ |
|
128 | - if($session[0] == '6' || $session[0] == '7'){ |
|
129 | - return $session[strlen($session) -1]; |
|
130 | - }else if($session[0] == '5' || $session[0] == '8'){ |
|
131 | - return $session[5]; |
|
132 | - } |
|
133 | - } |
|
134 | - return null; |
|
135 | - } |
|
136 | - |
|
137 | - private function getEnvBySessionFlag($targetConfig,$flag){ |
|
138 | - if($flag){ |
|
139 | - $userConf = ClusterTopClient::$dnsconfig['user']; |
|
140 | - $cfgArry = $userConf[$targetConfig]; |
|
141 | - foreach ($cfgArry as $key => $value) { |
|
142 | - if(in_array($flag,$value)) |
|
143 | - return $key; |
|
144 | - } |
|
145 | - }else{ |
|
146 | - return null; |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - private function getRandomWeightElement($elements){ |
|
151 | - $totalWeight = 0; |
|
152 | - if($elements){ |
|
153 | - foreach ($elements as $ele) { |
|
154 | - $weight = $this->getElementWeight($ele); |
|
155 | - $r = $this->randomFloat() * ($weight + $totalWeight); |
|
156 | - if($r >= $totalWeight){ |
|
157 | - $selected = $ele; |
|
158 | - } |
|
159 | - $totalWeight += $weight; |
|
160 | - } |
|
161 | - if($selected){ |
|
162 | - return $this->getElementValue($selected); |
|
163 | - } |
|
164 | - } |
|
165 | - return null; |
|
166 | - |
|
167 | - } |
|
168 | - |
|
169 | - private function getElementWeight($ele){ |
|
170 | - $params = explode('|', $ele); |
|
171 | - return floatval($params[1]); |
|
172 | - } |
|
173 | - private function getElementValue($ele){ |
|
174 | - $params = explode('|', $ele); |
|
175 | - return $params[0]; |
|
176 | - } |
|
177 | - |
|
178 | - private function getDeafultEnv(){ |
|
179 | - return ClusterTopClient::$dnsconfig['config']['def_env']; |
|
180 | - } |
|
181 | - |
|
182 | - private static function startsWith($haystack, $needle) { |
|
183 | - return $needle === "" || strpos($haystack, $needle) === 0; |
|
184 | - } |
|
185 | - |
|
186 | - private function object_to_array($obj) |
|
187 | - { |
|
188 | - $_arr= is_object($obj) ? get_object_vars($obj) : $obj; |
|
189 | - foreach($_arr as $key=> $val) |
|
190 | - { |
|
191 | - $val= (is_array($val) || is_object($val))? $this->object_to_array($val) : $val; |
|
192 | - $arr[$key] = $val; |
|
193 | - } |
|
194 | - return$arr; |
|
195 | - } |
|
196 | - |
|
197 | - private function randomFloat($min = 0, $max = 1) { return $min + mt_rand() / mt_getrandmax() * ($max - $min); } |
|
101 | + if($vip){ |
|
102 | + return $urlSchema['scheme']."://".$vip.$urlSchema['path']; |
|
103 | + } |
|
104 | + return null; |
|
105 | + } |
|
106 | + |
|
107 | + private function getEnvByApiName($apiName,$session=""){ |
|
108 | + $apiCfgArray = ClusterTopClient::$dnsconfig['api']; |
|
109 | + if($apiCfgArray){ |
|
110 | + if(array_key_exists($apiName,$apiCfgArray)){ |
|
111 | + $apiCfg = $apiCfgArray[$apiName]; |
|
112 | + if(array_key_exists('user',$apiCfg)){ |
|
113 | + $userFlag = $apiCfg['user']; |
|
114 | + $flag = $this->getUserFlag($session); |
|
115 | + if($userFlag && $flag ){ |
|
116 | + return $this->getEnvBySessionFlag($userFlag,$flag); |
|
117 | + }else{ |
|
118 | + return $this->getRandomWeightElement($apiCfg['rule']); |
|
119 | + } |
|
120 | + } |
|
121 | + } |
|
122 | + } |
|
123 | + return $this->getDeafultEnv(); |
|
124 | + } |
|
125 | + |
|
126 | + private function getUserFlag($session){ |
|
127 | + if($session && strlen($session) > 5){ |
|
128 | + if($session[0] == '6' || $session[0] == '7'){ |
|
129 | + return $session[strlen($session) -1]; |
|
130 | + }else if($session[0] == '5' || $session[0] == '8'){ |
|
131 | + return $session[5]; |
|
132 | + } |
|
133 | + } |
|
134 | + return null; |
|
135 | + } |
|
136 | + |
|
137 | + private function getEnvBySessionFlag($targetConfig,$flag){ |
|
138 | + if($flag){ |
|
139 | + $userConf = ClusterTopClient::$dnsconfig['user']; |
|
140 | + $cfgArry = $userConf[$targetConfig]; |
|
141 | + foreach ($cfgArry as $key => $value) { |
|
142 | + if(in_array($flag,$value)) |
|
143 | + return $key; |
|
144 | + } |
|
145 | + }else{ |
|
146 | + return null; |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + private function getRandomWeightElement($elements){ |
|
151 | + $totalWeight = 0; |
|
152 | + if($elements){ |
|
153 | + foreach ($elements as $ele) { |
|
154 | + $weight = $this->getElementWeight($ele); |
|
155 | + $r = $this->randomFloat() * ($weight + $totalWeight); |
|
156 | + if($r >= $totalWeight){ |
|
157 | + $selected = $ele; |
|
158 | + } |
|
159 | + $totalWeight += $weight; |
|
160 | + } |
|
161 | + if($selected){ |
|
162 | + return $this->getElementValue($selected); |
|
163 | + } |
|
164 | + } |
|
165 | + return null; |
|
166 | + |
|
167 | + } |
|
168 | + |
|
169 | + private function getElementWeight($ele){ |
|
170 | + $params = explode('|', $ele); |
|
171 | + return floatval($params[1]); |
|
172 | + } |
|
173 | + private function getElementValue($ele){ |
|
174 | + $params = explode('|', $ele); |
|
175 | + return $params[0]; |
|
176 | + } |
|
177 | + |
|
178 | + private function getDeafultEnv(){ |
|
179 | + return ClusterTopClient::$dnsconfig['config']['def_env']; |
|
180 | + } |
|
181 | + |
|
182 | + private static function startsWith($haystack, $needle) { |
|
183 | + return $needle === "" || strpos($haystack, $needle) === 0; |
|
184 | + } |
|
185 | + |
|
186 | + private function object_to_array($obj) |
|
187 | + { |
|
188 | + $_arr= is_object($obj) ? get_object_vars($obj) : $obj; |
|
189 | + foreach($_arr as $key=> $val) |
|
190 | + { |
|
191 | + $val= (is_array($val) || is_object($val))? $this->object_to_array($val) : $val; |
|
192 | + $arr[$key] = $val; |
|
193 | + } |
|
194 | + return$arr; |
|
195 | + } |
|
196 | + |
|
197 | + private function randomFloat($min = 0, $max = 1) { return $min + mt_rand() / mt_getrandmax() * ($max - $min); } |
|
198 | 198 | } |
199 | 199 | ?> |
200 | 200 | \ No newline at end of file |
@@ -3,56 +3,56 @@ discard block |
||
3 | 3 | |
4 | 4 | private static $dnsconfig; |
5 | 5 | private static $syncDate = 0; |
6 | - private static $applicationVar ; |
|
6 | + private static $applicationVar; |
|
7 | 7 | private static $cfgDuration = 10; |
8 | 8 | |
9 | - public function __construct($appkey = "",$secretKey = ""){ |
|
9 | + public function __construct($appkey = "", $secretKey = "") { |
|
10 | 10 | ClusterTopClient::$applicationVar = new ApplicationVar; |
11 | 11 | $this->appkey = $appkey; |
12 | - $this->secretKey = $secretKey ; |
|
12 | + $this->secretKey = $secretKey; |
|
13 | 13 | $saveConfig = ClusterTopClient::$applicationVar->getValue(); |
14 | 14 | |
15 | - if($saveConfig){ |
|
15 | + if ($saveConfig) { |
|
16 | 16 | $tmpConfig = $saveConfig['dnsconfig']; |
17 | 17 | ClusterTopClient::$dnsconfig = $this->object_to_array($tmpConfig); |
18 | 18 | unset($tmpConfig); |
19 | 19 | |
20 | 20 | ClusterTopClient::$syncDate = $saveConfig['syncDate']; |
21 | - if(!ClusterTopClient::$syncDate) |
|
21 | + if (!ClusterTopClient::$syncDate) |
|
22 | 22 | ClusterTopClient::$syncDate = 0; |
23 | 23 | } |
24 | 24 | } |
25 | 25 | |
26 | - public function __destruct(){ |
|
27 | - if(ClusterTopClient::$dnsconfig && ClusterTopClient::$syncDate){ |
|
28 | - ClusterTopClient::$applicationVar->setValue("dnsconfig",ClusterTopClient::$dnsconfig); |
|
29 | - ClusterTopClient::$applicationVar->setValue("syncDate",ClusterTopClient::$syncDate); |
|
26 | + public function __destruct() { |
|
27 | + if (ClusterTopClient::$dnsconfig && ClusterTopClient::$syncDate) { |
|
28 | + ClusterTopClient::$applicationVar->setValue("dnsconfig", ClusterTopClient::$dnsconfig); |
|
29 | + ClusterTopClient::$applicationVar->setValue("syncDate", ClusterTopClient::$syncDate); |
|
30 | 30 | ClusterTopClient::$applicationVar->write(); |
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
34 | - public function execute($request = null, $session = null,$bestUrl = null){ |
|
34 | + public function execute($request = null, $session = null, $bestUrl = null) { |
|
35 | 35 | $currentDate = date('U'); |
36 | 36 | $syncDuration = $this->getDnsConfigSyncDuration(); |
37 | - $bestUrl = $this->getBestVipUrl($this->gatewayUrl,$request->getApiMethodName(),$session); |
|
38 | - if($currentDate - ClusterTopClient::$syncDate > $syncDuration * 60){ |
|
37 | + $bestUrl = $this->getBestVipUrl($this->gatewayUrl, $request->getApiMethodName(), $session); |
|
38 | + if ($currentDate-ClusterTopClient::$syncDate > $syncDuration*60) { |
|
39 | 39 | $httpdns = new HttpdnsGetRequest; |
40 | - ClusterTopClient::$dnsconfig = json_decode(parent::execute($httpdns,null,$bestUrl)->result,true); |
|
40 | + ClusterTopClient::$dnsconfig = json_decode(parent::execute($httpdns, null, $bestUrl)->result, true); |
|
41 | 41 | $syncDate = date('U'); |
42 | - ClusterTopClient::$syncDate = $syncDate ; |
|
42 | + ClusterTopClient::$syncDate = $syncDate; |
|
43 | 43 | } |
44 | - return parent::execute($request,$session,$bestUrl); |
|
44 | + return parent::execute($request, $session, $bestUrl); |
|
45 | 45 | } |
46 | 46 | |
47 | - private function getDnsConfigSyncDuration(){ |
|
48 | - if(ClusterTopClient::$cfgDuration){ |
|
47 | + private function getDnsConfigSyncDuration() { |
|
48 | + if (ClusterTopClient::$cfgDuration) { |
|
49 | 49 | return ClusterTopClient::$cfgDuration; |
50 | 50 | } |
51 | - if(!ClusterTopClient::$dnsconfig){ |
|
51 | + if (!ClusterTopClient::$dnsconfig) { |
|
52 | 52 | return ClusterTopClient::$cfgDuration; |
53 | 53 | } |
54 | 54 | $config = json_encode(ClusterTopClient::$dnsconfig); |
55 | - if(!$config){ |
|
55 | + if (!$config) { |
|
56 | 56 | return ClusterTopClient::$cfgDuration; |
57 | 57 | } |
58 | 58 | $config = ClusterTopClient::$dnsconfig['config']; |
@@ -62,59 +62,59 @@ discard block |
||
62 | 62 | return ClusterTopClient::$cfgDuration; |
63 | 63 | } |
64 | 64 | |
65 | - private function getBestVipUrl($url,$apiname = null,$session = null){ |
|
65 | + private function getBestVipUrl($url, $apiname = null, $session = null) { |
|
66 | 66 | $config = ClusterTopClient::$dnsconfig['config']; |
67 | 67 | $degrade = $config['degrade']; |
68 | - if(strcmp($degrade,'true') == 0){ |
|
68 | + if (strcmp($degrade, 'true') == 0) { |
|
69 | 69 | return $url; |
70 | 70 | } |
71 | - $currentEnv = $this->getEnvByApiName($apiname,$session); |
|
72 | - $vip = $this->getVipByEnv($url,$currentEnv); |
|
73 | - if($vip) |
|
71 | + $currentEnv = $this->getEnvByApiName($apiname, $session); |
|
72 | + $vip = $this->getVipByEnv($url, $currentEnv); |
|
73 | + if ($vip) |
|
74 | 74 | return $vip; |
75 | 75 | return $url; |
76 | 76 | } |
77 | 77 | |
78 | - private function getVipByEnv($comUrl,$currentEnv){ |
|
78 | + private function getVipByEnv($comUrl, $currentEnv) { |
|
79 | 79 | $urlSchema = parse_url($comUrl); |
80 | - if(!$urlSchema) |
|
80 | + if (!$urlSchema) |
|
81 | 81 | return null; |
82 | - if(!ClusterTopClient::$dnsconfig['env']) |
|
82 | + if (!ClusterTopClient::$dnsconfig['env']) |
|
83 | 83 | return null; |
84 | 84 | |
85 | - if(!array_key_exists($currentEnv,ClusterTopClient::$dnsconfig['env'])) |
|
85 | + if (!array_key_exists($currentEnv, ClusterTopClient::$dnsconfig['env'])) |
|
86 | 86 | return null; |
87 | 87 | |
88 | 88 | $hostList = ClusterTopClient::$dnsconfig['env'][$currentEnv]; |
89 | - if(!$hostList) |
|
90 | - return null ; |
|
89 | + if (!$hostList) |
|
90 | + return null; |
|
91 | 91 | |
92 | 92 | $vipList = null; |
93 | 93 | foreach ($hostList as $key => $value) { |
94 | - if(strcmp($key,$urlSchema['host']) == 0 && strcmp($value['proto'],$urlSchema['scheme']) == 0){ |
|
94 | + if (strcmp($key, $urlSchema['host']) == 0 && strcmp($value['proto'], $urlSchema['scheme']) == 0) { |
|
95 | 95 | $vipList = $value; |
96 | 96 | break; |
97 | 97 | } |
98 | 98 | } |
99 | 99 | $vip = $this->getRandomWeightElement($vipList['vip']); |
100 | 100 | |
101 | - if($vip){ |
|
101 | + if ($vip) { |
|
102 | 102 | return $urlSchema['scheme']."://".$vip.$urlSchema['path']; |
103 | 103 | } |
104 | 104 | return null; |
105 | 105 | } |
106 | 106 | |
107 | - private function getEnvByApiName($apiName,$session=""){ |
|
107 | + private function getEnvByApiName($apiName, $session = "") { |
|
108 | 108 | $apiCfgArray = ClusterTopClient::$dnsconfig['api']; |
109 | - if($apiCfgArray){ |
|
110 | - if(array_key_exists($apiName,$apiCfgArray)){ |
|
109 | + if ($apiCfgArray) { |
|
110 | + if (array_key_exists($apiName, $apiCfgArray)) { |
|
111 | 111 | $apiCfg = $apiCfgArray[$apiName]; |
112 | - if(array_key_exists('user',$apiCfg)){ |
|
112 | + if (array_key_exists('user', $apiCfg)) { |
|
113 | 113 | $userFlag = $apiCfg['user']; |
114 | 114 | $flag = $this->getUserFlag($session); |
115 | - if($userFlag && $flag ){ |
|
116 | - return $this->getEnvBySessionFlag($userFlag,$flag); |
|
117 | - }else{ |
|
115 | + if ($userFlag && $flag) { |
|
116 | + return $this->getEnvBySessionFlag($userFlag, $flag); |
|
117 | + }else { |
|
118 | 118 | return $this->getRandomWeightElement($apiCfg['rule']); |
119 | 119 | } |
120 | 120 | } |
@@ -123,42 +123,42 @@ discard block |
||
123 | 123 | return $this->getDeafultEnv(); |
124 | 124 | } |
125 | 125 | |
126 | - private function getUserFlag($session){ |
|
127 | - if($session && strlen($session) > 5){ |
|
128 | - if($session[0] == '6' || $session[0] == '7'){ |
|
129 | - return $session[strlen($session) -1]; |
|
130 | - }else if($session[0] == '5' || $session[0] == '8'){ |
|
126 | + private function getUserFlag($session) { |
|
127 | + if ($session && strlen($session) > 5) { |
|
128 | + if ($session[0] == '6' || $session[0] == '7') { |
|
129 | + return $session[strlen($session)-1]; |
|
130 | + }else if ($session[0] == '5' || $session[0] == '8') { |
|
131 | 131 | return $session[5]; |
132 | 132 | } |
133 | 133 | } |
134 | 134 | return null; |
135 | 135 | } |
136 | 136 | |
137 | - private function getEnvBySessionFlag($targetConfig,$flag){ |
|
138 | - if($flag){ |
|
137 | + private function getEnvBySessionFlag($targetConfig, $flag) { |
|
138 | + if ($flag) { |
|
139 | 139 | $userConf = ClusterTopClient::$dnsconfig['user']; |
140 | 140 | $cfgArry = $userConf[$targetConfig]; |
141 | 141 | foreach ($cfgArry as $key => $value) { |
142 | - if(in_array($flag,$value)) |
|
142 | + if (in_array($flag, $value)) |
|
143 | 143 | return $key; |
144 | 144 | } |
145 | - }else{ |
|
145 | + }else { |
|
146 | 146 | return null; |
147 | 147 | } |
148 | 148 | } |
149 | 149 | |
150 | - private function getRandomWeightElement($elements){ |
|
150 | + private function getRandomWeightElement($elements) { |
|
151 | 151 | $totalWeight = 0; |
152 | - if($elements){ |
|
152 | + if ($elements) { |
|
153 | 153 | foreach ($elements as $ele) { |
154 | 154 | $weight = $this->getElementWeight($ele); |
155 | - $r = $this->randomFloat() * ($weight + $totalWeight); |
|
156 | - if($r >= $totalWeight){ |
|
155 | + $r = $this->randomFloat()*($weight+$totalWeight); |
|
156 | + if ($r >= $totalWeight) { |
|
157 | 157 | $selected = $ele; |
158 | 158 | } |
159 | 159 | $totalWeight += $weight; |
160 | 160 | } |
161 | - if($selected){ |
|
161 | + if ($selected) { |
|
162 | 162 | return $this->getElementValue($selected); |
163 | 163 | } |
164 | 164 | } |
@@ -166,16 +166,16 @@ discard block |
||
166 | 166 | |
167 | 167 | } |
168 | 168 | |
169 | - private function getElementWeight($ele){ |
|
169 | + private function getElementWeight($ele) { |
|
170 | 170 | $params = explode('|', $ele); |
171 | 171 | return floatval($params[1]); |
172 | 172 | } |
173 | - private function getElementValue($ele){ |
|
173 | + private function getElementValue($ele) { |
|
174 | 174 | $params = explode('|', $ele); |
175 | 175 | return $params[0]; |
176 | 176 | } |
177 | 177 | |
178 | - private function getDeafultEnv(){ |
|
178 | + private function getDeafultEnv() { |
|
179 | 179 | return ClusterTopClient::$dnsconfig['config']['def_env']; |
180 | 180 | } |
181 | 181 | |
@@ -185,15 +185,15 @@ discard block |
||
185 | 185 | |
186 | 186 | private function object_to_array($obj) |
187 | 187 | { |
188 | - $_arr= is_object($obj) ? get_object_vars($obj) : $obj; |
|
189 | - foreach($_arr as $key=> $val) |
|
188 | + $_arr = is_object($obj) ? get_object_vars($obj) : $obj; |
|
189 | + foreach ($_arr as $key=> $val) |
|
190 | 190 | { |
191 | - $val= (is_array($val) || is_object($val))? $this->object_to_array($val) : $val; |
|
191 | + $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val) : $val; |
|
192 | 192 | $arr[$key] = $val; |
193 | 193 | } |
194 | 194 | return$arr; |
195 | 195 | } |
196 | 196 | |
197 | - private function randomFloat($min = 0, $max = 1) { return $min + mt_rand() / mt_getrandmax() * ($max - $min); } |
|
197 | + private function randomFloat($min = 0, $max = 1) { return $min+mt_rand()/mt_getrandmax()*($max-$min); } |
|
198 | 198 | } |
199 | 199 | ?> |
200 | 200 | \ No newline at end of file |
@@ -18,8 +18,9 @@ discard block |
||
18 | 18 | unset($tmpConfig); |
19 | 19 | |
20 | 20 | ClusterTopClient::$syncDate = $saveConfig['syncDate']; |
21 | - if(!ClusterTopClient::$syncDate) |
|
22 | - ClusterTopClient::$syncDate = 0; |
|
21 | + if(!ClusterTopClient::$syncDate) { |
|
22 | + ClusterTopClient::$syncDate = 0; |
|
23 | + } |
|
23 | 24 | } |
24 | 25 | } |
25 | 26 | |
@@ -70,24 +71,29 @@ discard block |
||
70 | 71 | } |
71 | 72 | $currentEnv = $this->getEnvByApiName($apiname,$session); |
72 | 73 | $vip = $this->getVipByEnv($url,$currentEnv); |
73 | - if($vip) |
|
74 | - return $vip; |
|
74 | + if($vip) { |
|
75 | + return $vip; |
|
76 | + } |
|
75 | 77 | return $url; |
76 | 78 | } |
77 | 79 | |
78 | 80 | private function getVipByEnv($comUrl,$currentEnv){ |
79 | 81 | $urlSchema = parse_url($comUrl); |
80 | - if(!$urlSchema) |
|
81 | - return null; |
|
82 | - if(!ClusterTopClient::$dnsconfig['env']) |
|
83 | - return null; |
|
82 | + if(!$urlSchema) { |
|
83 | + return null; |
|
84 | + } |
|
85 | + if(!ClusterTopClient::$dnsconfig['env']) { |
|
86 | + return null; |
|
87 | + } |
|
84 | 88 | |
85 | - if(!array_key_exists($currentEnv,ClusterTopClient::$dnsconfig['env'])) |
|
86 | - return null; |
|
89 | + if(!array_key_exists($currentEnv,ClusterTopClient::$dnsconfig['env'])) { |
|
90 | + return null; |
|
91 | + } |
|
87 | 92 | |
88 | 93 | $hostList = ClusterTopClient::$dnsconfig['env'][$currentEnv]; |
89 | - if(!$hostList) |
|
90 | - return null ; |
|
94 | + if(!$hostList) { |
|
95 | + return null ; |
|
96 | + } |
|
91 | 97 | |
92 | 98 | $vipList = null; |
93 | 99 | foreach ($hostList as $key => $value) { |
@@ -114,7 +120,7 @@ discard block |
||
114 | 120 | $flag = $this->getUserFlag($session); |
115 | 121 | if($userFlag && $flag ){ |
116 | 122 | return $this->getEnvBySessionFlag($userFlag,$flag); |
117 | - }else{ |
|
123 | + } else{ |
|
118 | 124 | return $this->getRandomWeightElement($apiCfg['rule']); |
119 | 125 | } |
120 | 126 | } |
@@ -127,7 +133,7 @@ discard block |
||
127 | 133 | if($session && strlen($session) > 5){ |
128 | 134 | if($session[0] == '6' || $session[0] == '7'){ |
129 | 135 | return $session[strlen($session) -1]; |
130 | - }else if($session[0] == '5' || $session[0] == '8'){ |
|
136 | + } else if($session[0] == '5' || $session[0] == '8'){ |
|
131 | 137 | return $session[5]; |
132 | 138 | } |
133 | 139 | } |
@@ -139,10 +145,11 @@ discard block |
||
139 | 145 | $userConf = ClusterTopClient::$dnsconfig['user']; |
140 | 146 | $cfgArry = $userConf[$targetConfig]; |
141 | 147 | foreach ($cfgArry as $key => $value) { |
142 | - if(in_array($flag,$value)) |
|
143 | - return $key; |
|
148 | + if(in_array($flag,$value)) { |
|
149 | + return $key; |
|
150 | + } |
|
144 | 151 | } |
145 | - }else{ |
|
152 | + } else{ |
|
146 | 153 | return null; |
147 | 154 | } |
148 | 155 | } |
@@ -1,255 +1,255 @@ |
||
1 | 1 | <?php |
2 | 2 | class AliyunClient |
3 | 3 | { |
4 | - public $accessKeyId; |
|
4 | + public $accessKeyId; |
|
5 | 5 | |
6 | - public $accessKeySecret; |
|
6 | + public $accessKeySecret; |
|
7 | 7 | |
8 | - public $serverUrl = "http://ecs.aliyuncs.com/"; |
|
8 | + public $serverUrl = "http://ecs.aliyuncs.com/"; |
|
9 | 9 | |
10 | - public $format = "json"; |
|
10 | + public $format = "json"; |
|
11 | 11 | |
12 | - public $connectTimeout = 3000;//3秒 |
|
12 | + public $connectTimeout = 3000;//3秒 |
|
13 | 13 | |
14 | - public $readTimeout = 80000;//80秒 |
|
14 | + public $readTimeout = 80000;//80秒 |
|
15 | 15 | |
16 | - /** 是否打开入参check**/ |
|
17 | - public $checkRequest = true; |
|
16 | + /** 是否打开入参check**/ |
|
17 | + public $checkRequest = true; |
|
18 | 18 | |
19 | - protected $signatureMethod = "HMAC-SHA1"; |
|
19 | + protected $signatureMethod = "HMAC-SHA1"; |
|
20 | 20 | |
21 | - protected $signatureVersion = "1.0"; |
|
21 | + protected $signatureVersion = "1.0"; |
|
22 | 22 | |
23 | 23 | protected $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; // ISO8601规范 |
24 | 24 | |
25 | - protected $sdkVersion = "1.0"; |
|
26 | - |
|
27 | - public function execute($request) |
|
28 | - { |
|
29 | - if($this->checkRequest) { |
|
30 | - try { |
|
31 | - $request->check(); |
|
32 | - } catch (Exception $e) { |
|
33 | - $result->code = $e->getCode(); |
|
34 | - $result->message = $e->getMessage(); |
|
35 | - return $result; |
|
36 | - } |
|
37 | - } |
|
38 | - //获取业务参数 |
|
39 | - $apiParams = $request->getApiParas(); |
|
40 | - //组装系统参数 |
|
41 | - $apiParams["AccessKeyId"] = $this->accessKeyId; |
|
42 | - $apiParams["Format"] = $this->format;// |
|
43 | - $apiParams["SignatureMethod"] = $this->signatureMethod; |
|
44 | - $apiParams["SignatureVersion"] = $this->signatureVersion; |
|
45 | - $apiParams["SignatureNonce"] = uniqid(); |
|
46 | - date_default_timezone_set("GMT"); |
|
47 | - $apiParams["TimeStamp"] = date($this->dateTimeFormat); |
|
48 | - $apiParams["partner_id"] = $this->sdkVersion; |
|
25 | + protected $sdkVersion = "1.0"; |
|
26 | + |
|
27 | + public function execute($request) |
|
28 | + { |
|
29 | + if($this->checkRequest) { |
|
30 | + try { |
|
31 | + $request->check(); |
|
32 | + } catch (Exception $e) { |
|
33 | + $result->code = $e->getCode(); |
|
34 | + $result->message = $e->getMessage(); |
|
35 | + return $result; |
|
36 | + } |
|
37 | + } |
|
38 | + //获取业务参数 |
|
39 | + $apiParams = $request->getApiParas(); |
|
40 | + //组装系统参数 |
|
41 | + $apiParams["AccessKeyId"] = $this->accessKeyId; |
|
42 | + $apiParams["Format"] = $this->format;// |
|
43 | + $apiParams["SignatureMethod"] = $this->signatureMethod; |
|
44 | + $apiParams["SignatureVersion"] = $this->signatureVersion; |
|
45 | + $apiParams["SignatureNonce"] = uniqid(); |
|
46 | + date_default_timezone_set("GMT"); |
|
47 | + $apiParams["TimeStamp"] = date($this->dateTimeFormat); |
|
48 | + $apiParams["partner_id"] = $this->sdkVersion; |
|
49 | 49 | |
50 | - $apiNameArray = split("\.", $request->getApiMethodName()); |
|
51 | - $apiParams["Action"] = $apiNameArray[3]; |
|
52 | - $apiParams["Version"] = $apiNameArray[4]; |
|
53 | - //签名 |
|
54 | - $apiParams["Signature"] = $this->computeSignature($apiParams, $this->accessKeySecret); |
|
55 | - |
|
56 | - //系统参数放入GET请求串 |
|
57 | - $requestUrl = rtrim($this->serverUrl,"/") . "/?"; |
|
58 | - foreach ($apiParams as $apiParamKey => $apiParamValue) |
|
59 | - { |
|
60 | - $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&"; |
|
61 | - } |
|
62 | - $requestUrl = substr($requestUrl, 0, -1); |
|
63 | - //发起HTTP请求 |
|
64 | - try |
|
65 | - { |
|
66 | - $resp = $this->curl($requestUrl, null); |
|
67 | - } |
|
68 | - catch (Exception $e) |
|
69 | - { |
|
70 | - $this->logCommunicationError($apiParams["Action"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
|
71 | - if ("json" == $this->format) |
|
72 | - { |
|
73 | - return json_decode($e->getMessage()); |
|
74 | - } |
|
75 | - else if("xml" == $this->format) |
|
76 | - { |
|
77 | - return @simplexml_load_string($e->getMessage()); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - //解析API返回结果 |
|
82 | - $respWellFormed = false; |
|
83 | - if ("json" == $this->format) |
|
84 | - { |
|
85 | - $respObject = json_decode($resp); |
|
86 | - if (null !== $respObject) |
|
87 | - { |
|
88 | - $respWellFormed = true; |
|
89 | - } |
|
90 | - } |
|
91 | - else if("xml" == $this->format) |
|
92 | - { |
|
93 | - $respObject = @simplexml_load_string($resp); |
|
94 | - if (false !== $respObject) |
|
95 | - { |
|
96 | - $respWellFormed = true; |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
|
101 | - if (false === $respWellFormed) |
|
102 | - { |
|
103 | - $this->logCommunicationError($apiParams["Action"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
|
104 | - $result->code = 0; |
|
105 | - $result->message = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
|
106 | - return $result; |
|
107 | - } |
|
108 | - |
|
109 | - //如果TOP返回了错误码,记录到业务错误日志中 |
|
110 | - if (isset($respObject->code)) |
|
111 | - { |
|
112 | - $logger = new LtLogger; |
|
113 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
114 | - $logger->log(array( |
|
115 | - date("Y-m-d H:i:s"), |
|
116 | - $resp |
|
117 | - )); |
|
118 | - } |
|
119 | - return $respObject; |
|
120 | - } |
|
121 | - |
|
122 | - public function exec($paramsArray) |
|
123 | - { |
|
124 | - if (!isset($paramsArray["Action"])) |
|
125 | - { |
|
126 | - trigger_error("No api name passed"); |
|
127 | - } |
|
128 | - $inflector = new LtInflector; |
|
129 | - $inflector->conf["separator"] = "."; |
|
130 | - $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["Action"], 7))) . "Request"; |
|
131 | - if (!class_exists($requestClassName)) |
|
132 | - { |
|
133 | - trigger_error("No such api: " . $paramsArray["Action"]); |
|
134 | - } |
|
135 | - |
|
136 | - $req = new $requestClassName; |
|
137 | - foreach($paramsArray as $paraKey => $paraValue) |
|
138 | - { |
|
139 | - $inflector->conf["separator"] = "_"; |
|
140 | - $setterMethodName = $inflector->camelize($paraKey); |
|
141 | - $inflector->conf["separator"] = "."; |
|
142 | - $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
|
143 | - if (method_exists($req, $setterMethodName)) |
|
144 | - { |
|
145 | - $req->$setterMethodName($paraValue); |
|
146 | - } |
|
147 | - } |
|
148 | - return $this->execute($req, $session); |
|
149 | - } |
|
50 | + $apiNameArray = split("\.", $request->getApiMethodName()); |
|
51 | + $apiParams["Action"] = $apiNameArray[3]; |
|
52 | + $apiParams["Version"] = $apiNameArray[4]; |
|
53 | + //签名 |
|
54 | + $apiParams["Signature"] = $this->computeSignature($apiParams, $this->accessKeySecret); |
|
55 | + |
|
56 | + //系统参数放入GET请求串 |
|
57 | + $requestUrl = rtrim($this->serverUrl,"/") . "/?"; |
|
58 | + foreach ($apiParams as $apiParamKey => $apiParamValue) |
|
59 | + { |
|
60 | + $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&"; |
|
61 | + } |
|
62 | + $requestUrl = substr($requestUrl, 0, -1); |
|
63 | + //发起HTTP请求 |
|
64 | + try |
|
65 | + { |
|
66 | + $resp = $this->curl($requestUrl, null); |
|
67 | + } |
|
68 | + catch (Exception $e) |
|
69 | + { |
|
70 | + $this->logCommunicationError($apiParams["Action"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
|
71 | + if ("json" == $this->format) |
|
72 | + { |
|
73 | + return json_decode($e->getMessage()); |
|
74 | + } |
|
75 | + else if("xml" == $this->format) |
|
76 | + { |
|
77 | + return @simplexml_load_string($e->getMessage()); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + //解析API返回结果 |
|
82 | + $respWellFormed = false; |
|
83 | + if ("json" == $this->format) |
|
84 | + { |
|
85 | + $respObject = json_decode($resp); |
|
86 | + if (null !== $respObject) |
|
87 | + { |
|
88 | + $respWellFormed = true; |
|
89 | + } |
|
90 | + } |
|
91 | + else if("xml" == $this->format) |
|
92 | + { |
|
93 | + $respObject = @simplexml_load_string($resp); |
|
94 | + if (false !== $respObject) |
|
95 | + { |
|
96 | + $respWellFormed = true; |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
|
101 | + if (false === $respWellFormed) |
|
102 | + { |
|
103 | + $this->logCommunicationError($apiParams["Action"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
|
104 | + $result->code = 0; |
|
105 | + $result->message = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
|
106 | + return $result; |
|
107 | + } |
|
108 | + |
|
109 | + //如果TOP返回了错误码,记录到业务错误日志中 |
|
110 | + if (isset($respObject->code)) |
|
111 | + { |
|
112 | + $logger = new LtLogger; |
|
113 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
114 | + $logger->log(array( |
|
115 | + date("Y-m-d H:i:s"), |
|
116 | + $resp |
|
117 | + )); |
|
118 | + } |
|
119 | + return $respObject; |
|
120 | + } |
|
121 | + |
|
122 | + public function exec($paramsArray) |
|
123 | + { |
|
124 | + if (!isset($paramsArray["Action"])) |
|
125 | + { |
|
126 | + trigger_error("No api name passed"); |
|
127 | + } |
|
128 | + $inflector = new LtInflector; |
|
129 | + $inflector->conf["separator"] = "."; |
|
130 | + $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["Action"], 7))) . "Request"; |
|
131 | + if (!class_exists($requestClassName)) |
|
132 | + { |
|
133 | + trigger_error("No such api: " . $paramsArray["Action"]); |
|
134 | + } |
|
135 | + |
|
136 | + $req = new $requestClassName; |
|
137 | + foreach($paramsArray as $paraKey => $paraValue) |
|
138 | + { |
|
139 | + $inflector->conf["separator"] = "_"; |
|
140 | + $setterMethodName = $inflector->camelize($paraKey); |
|
141 | + $inflector->conf["separator"] = "."; |
|
142 | + $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
|
143 | + if (method_exists($req, $setterMethodName)) |
|
144 | + { |
|
145 | + $req->$setterMethodName($paraValue); |
|
146 | + } |
|
147 | + } |
|
148 | + return $this->execute($req, $session); |
|
149 | + } |
|
150 | 150 | |
151 | - protected function percentEncode($str) |
|
152 | - { |
|
153 | - // 使用urlencode编码后,将"+","*","%7E"做替换即满足 API规定的编码规范 |
|
154 | - $res = urlencode($str); |
|
155 | - $res = preg_replace('/\+/', '%20', $res); |
|
156 | - $res = preg_replace('/\*/', '%2A', $res); |
|
157 | - $res = preg_replace('/%7E/', '~', $res); |
|
158 | - return $res; |
|
159 | - } |
|
151 | + protected function percentEncode($str) |
|
152 | + { |
|
153 | + // 使用urlencode编码后,将"+","*","%7E"做替换即满足 API规定的编码规范 |
|
154 | + $res = urlencode($str); |
|
155 | + $res = preg_replace('/\+/', '%20', $res); |
|
156 | + $res = preg_replace('/\*/', '%2A', $res); |
|
157 | + $res = preg_replace('/%7E/', '~', $res); |
|
158 | + return $res; |
|
159 | + } |
|
160 | 160 | |
161 | - protected function computeSignature($parameters, $accessKeySecret) |
|
162 | - { |
|
163 | - // 将参数Key按字典顺序排序 |
|
164 | - ksort($parameters); |
|
161 | + protected function computeSignature($parameters, $accessKeySecret) |
|
162 | + { |
|
163 | + // 将参数Key按字典顺序排序 |
|
164 | + ksort($parameters); |
|
165 | 165 | |
166 | - // 生成规范化请求字符串 |
|
167 | - $canonicalizedQueryString = ''; |
|
168 | - foreach($parameters as $key => $value) |
|
169 | - { |
|
170 | - $canonicalizedQueryString .= '&' . $this->percentEncode($key) |
|
171 | - . '=' . $this->percentEncode($value); |
|
172 | - } |
|
166 | + // 生成规范化请求字符串 |
|
167 | + $canonicalizedQueryString = ''; |
|
168 | + foreach($parameters as $key => $value) |
|
169 | + { |
|
170 | + $canonicalizedQueryString .= '&' . $this->percentEncode($key) |
|
171 | + . '=' . $this->percentEncode($value); |
|
172 | + } |
|
173 | 173 | |
174 | - // 生成用于计算签名的字符串 stringToSign |
|
175 | - $stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1)); |
|
174 | + // 生成用于计算签名的字符串 stringToSign |
|
175 | + $stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1)); |
|
176 | 176 | |
177 | - // 计算签名,注意accessKeySecret后面要加上字符'&' |
|
178 | - $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true)); |
|
179 | - return $signature; |
|
180 | - } |
|
177 | + // 计算签名,注意accessKeySecret后面要加上字符'&' |
|
178 | + $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true)); |
|
179 | + return $signature; |
|
180 | + } |
|
181 | 181 | |
182 | 182 | public function curl($url, $postFields = null) |
183 | - { |
|
184 | - $ch = curl_init(); |
|
185 | - curl_setopt($ch, CURLOPT_URL, $url); |
|
186 | - curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
187 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
188 | - if ($this->readTimeout) { |
|
189 | - curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
190 | - } |
|
191 | - if ($this->connectTimeout) { |
|
192 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
193 | - } |
|
194 | - //https 请求 |
|
195 | - if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
196 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
197 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
198 | - } |
|
199 | - |
|
200 | - if (is_array($postFields) && 0 < count($postFields)) |
|
201 | - { |
|
202 | - $postBodyString = ""; |
|
203 | - $postMultipart = false; |
|
204 | - foreach ($postFields as $k => $v) |
|
205 | - { |
|
206 | - if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
207 | - { |
|
208 | - $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
209 | - } |
|
210 | - else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
211 | - { |
|
212 | - $postMultipart = true; |
|
213 | - } |
|
214 | - } |
|
215 | - unset($k, $v); |
|
216 | - curl_setopt($ch, CURLOPT_POST, true); |
|
217 | - if ($postMultipart) |
|
218 | - { |
|
219 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
|
220 | - } |
|
221 | - else |
|
222 | - { |
|
223 | - curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
|
224 | - } |
|
225 | - } |
|
226 | - $reponse = curl_exec($ch); |
|
183 | + { |
|
184 | + $ch = curl_init(); |
|
185 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
186 | + curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|
187 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
188 | + if ($this->readTimeout) { |
|
189 | + curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
|
190 | + } |
|
191 | + if ($this->connectTimeout) { |
|
192 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
|
193 | + } |
|
194 | + //https 请求 |
|
195 | + if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
196 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
197 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
198 | + } |
|
199 | + |
|
200 | + if (is_array($postFields) && 0 < count($postFields)) |
|
201 | + { |
|
202 | + $postBodyString = ""; |
|
203 | + $postMultipart = false; |
|
204 | + foreach ($postFields as $k => $v) |
|
205 | + { |
|
206 | + if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
207 | + { |
|
208 | + $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
209 | + } |
|
210 | + else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
211 | + { |
|
212 | + $postMultipart = true; |
|
213 | + } |
|
214 | + } |
|
215 | + unset($k, $v); |
|
216 | + curl_setopt($ch, CURLOPT_POST, true); |
|
217 | + if ($postMultipart) |
|
218 | + { |
|
219 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
|
220 | + } |
|
221 | + else |
|
222 | + { |
|
223 | + curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
|
224 | + } |
|
225 | + } |
|
226 | + $reponse = curl_exec($ch); |
|
227 | 227 | |
228 | - if (curl_errno($ch)) |
|
229 | - { |
|
230 | - throw new Exception(curl_error($ch),0); |
|
231 | - } |
|
232 | - curl_close($ch); |
|
233 | - return $reponse; |
|
234 | - } |
|
235 | - |
|
236 | - protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt) |
|
237 | - { |
|
238 | - $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
239 | - $logger = new LtLogger; |
|
240 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->accessKeyId . "_" . date("Y-m-d") . ".log"; |
|
241 | - $logger->conf["separator"] = "^_^"; |
|
242 | - $logData = array( |
|
243 | - date("Y-m-d H:i:s"), |
|
244 | - $apiName, |
|
245 | - $this->accessKeyId, |
|
246 | - $localIp, |
|
247 | - PHP_OS, |
|
248 | - $this->sdkVersion, |
|
249 | - $requestUrl, |
|
250 | - $errorCode, |
|
251 | - str_replace("\n","",$responseTxt) |
|
252 | - ); |
|
253 | - $logger->log($logData); |
|
254 | - } |
|
228 | + if (curl_errno($ch)) |
|
229 | + { |
|
230 | + throw new Exception(curl_error($ch),0); |
|
231 | + } |
|
232 | + curl_close($ch); |
|
233 | + return $reponse; |
|
234 | + } |
|
235 | + |
|
236 | + protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt) |
|
237 | + { |
|
238 | + $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
|
239 | + $logger = new LtLogger; |
|
240 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->accessKeyId . "_" . date("Y-m-d") . ".log"; |
|
241 | + $logger->conf["separator"] = "^_^"; |
|
242 | + $logData = array( |
|
243 | + date("Y-m-d H:i:s"), |
|
244 | + $apiName, |
|
245 | + $this->accessKeyId, |
|
246 | + $localIp, |
|
247 | + PHP_OS, |
|
248 | + $this->sdkVersion, |
|
249 | + $requestUrl, |
|
250 | + $errorCode, |
|
251 | + str_replace("\n","",$responseTxt) |
|
252 | + ); |
|
253 | + $logger->log($logData); |
|
254 | + } |
|
255 | 255 | } |
@@ -9,9 +9,9 @@ discard block |
||
9 | 9 | |
10 | 10 | public $format = "json"; |
11 | 11 | |
12 | - public $connectTimeout = 3000;//3秒 |
|
12 | + public $connectTimeout = 3000; //3秒 |
|
13 | 13 | |
14 | - public $readTimeout = 80000;//80秒 |
|
14 | + public $readTimeout = 80000; //80秒 |
|
15 | 15 | |
16 | 16 | /** 是否打开入参check**/ |
17 | 17 | public $checkRequest = true; |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | |
27 | 27 | public function execute($request) |
28 | 28 | { |
29 | - if($this->checkRequest) { |
|
29 | + if ($this->checkRequest) { |
|
30 | 30 | try { |
31 | 31 | $request->check(); |
32 | 32 | } catch (Exception $e) { |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | $apiParams = $request->getApiParas(); |
40 | 40 | //组装系统参数 |
41 | 41 | $apiParams["AccessKeyId"] = $this->accessKeyId; |
42 | - $apiParams["Format"] = $this->format;// |
|
42 | + $apiParams["Format"] = $this->format; // |
|
43 | 43 | $apiParams["SignatureMethod"] = $this->signatureMethod; |
44 | 44 | $apiParams["SignatureVersion"] = $this->signatureVersion; |
45 | 45 | $apiParams["SignatureNonce"] = uniqid(); |
@@ -47,17 +47,17 @@ discard block |
||
47 | 47 | $apiParams["TimeStamp"] = date($this->dateTimeFormat); |
48 | 48 | $apiParams["partner_id"] = $this->sdkVersion; |
49 | 49 | |
50 | - $apiNameArray = split("\.", $request->getApiMethodName()); |
|
50 | + $apiNameArray = split("\.", $request->getApiMethodName()); |
|
51 | 51 | $apiParams["Action"] = $apiNameArray[3]; |
52 | 52 | $apiParams["Version"] = $apiNameArray[4]; |
53 | 53 | //签名 |
54 | 54 | $apiParams["Signature"] = $this->computeSignature($apiParams, $this->accessKeySecret); |
55 | 55 | |
56 | 56 | //系统参数放入GET请求串 |
57 | - $requestUrl = rtrim($this->serverUrl,"/") . "/?"; |
|
57 | + $requestUrl = rtrim($this->serverUrl, "/")."/?"; |
|
58 | 58 | foreach ($apiParams as $apiParamKey => $apiParamValue) |
59 | 59 | { |
60 | - $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&"; |
|
60 | + $requestUrl .= "$apiParamKey=".urlencode($apiParamValue)."&"; |
|
61 | 61 | } |
62 | 62 | $requestUrl = substr($requestUrl, 0, -1); |
63 | 63 | //发起HTTP请求 |
@@ -67,12 +67,12 @@ discard block |
||
67 | 67 | } |
68 | 68 | catch (Exception $e) |
69 | 69 | { |
70 | - $this->logCommunicationError($apiParams["Action"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
|
70 | + $this->logCommunicationError($apiParams["Action"], $requestUrl, "HTTP_ERROR_".$e->getCode(), $e->getMessage()); |
|
71 | 71 | if ("json" == $this->format) |
72 | 72 | { |
73 | 73 | return json_decode($e->getMessage()); |
74 | 74 | } |
75 | - else if("xml" == $this->format) |
|
75 | + else if ("xml" == $this->format) |
|
76 | 76 | { |
77 | 77 | return @simplexml_load_string($e->getMessage()); |
78 | 78 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | $respWellFormed = true; |
89 | 89 | } |
90 | 90 | } |
91 | - else if("xml" == $this->format) |
|
91 | + else if ("xml" == $this->format) |
|
92 | 92 | { |
93 | 93 | $respObject = @simplexml_load_string($resp); |
94 | 94 | if (false !== $respObject) |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
101 | 101 | if (false === $respWellFormed) |
102 | 102 | { |
103 | - $this->logCommunicationError($apiParams["Action"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
|
103 | + $this->logCommunicationError($apiParams["Action"], $requestUrl, "HTTP_RESPONSE_NOT_WELL_FORMED", $resp); |
|
104 | 104 | $result->code = 0; |
105 | 105 | $result->message = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
106 | 106 | return $result; |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | if (isset($respObject->code)) |
111 | 111 | { |
112 | 112 | $logger = new LtLogger; |
113 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
|
113 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/').'/'."logs/top_biz_err_".$this->appkey."_".date("Y-m-d").".log"; |
|
114 | 114 | $logger->log(array( |
115 | 115 | date("Y-m-d H:i:s"), |
116 | 116 | $resp |
@@ -127,19 +127,19 @@ discard block |
||
127 | 127 | } |
128 | 128 | $inflector = new LtInflector; |
129 | 129 | $inflector->conf["separator"] = "."; |
130 | - $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["Action"], 7))) . "Request"; |
|
130 | + $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["Action"], 7)))."Request"; |
|
131 | 131 | if (!class_exists($requestClassName)) |
132 | 132 | { |
133 | - trigger_error("No such api: " . $paramsArray["Action"]); |
|
133 | + trigger_error("No such api: ".$paramsArray["Action"]); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | $req = new $requestClassName; |
137 | - foreach($paramsArray as $paraKey => $paraValue) |
|
137 | + foreach ($paramsArray as $paraKey => $paraValue) |
|
138 | 138 | { |
139 | 139 | $inflector->conf["separator"] = "_"; |
140 | 140 | $setterMethodName = $inflector->camelize($paraKey); |
141 | 141 | $inflector->conf["separator"] = "."; |
142 | - $setterMethodName = "set" . $inflector->camelize($setterMethodName); |
|
142 | + $setterMethodName = "set".$inflector->camelize($setterMethodName); |
|
143 | 143 | if (method_exists($req, $setterMethodName)) |
144 | 144 | { |
145 | 145 | $req->$setterMethodName($paraValue); |
@@ -165,17 +165,17 @@ discard block |
||
165 | 165 | |
166 | 166 | // 生成规范化请求字符串 |
167 | 167 | $canonicalizedQueryString = ''; |
168 | - foreach($parameters as $key => $value) |
|
168 | + foreach ($parameters as $key => $value) |
|
169 | 169 | { |
170 | - $canonicalizedQueryString .= '&' . $this->percentEncode($key) |
|
171 | - . '=' . $this->percentEncode($value); |
|
170 | + $canonicalizedQueryString .= '&'.$this->percentEncode($key) |
|
171 | + . '='.$this->percentEncode($value); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | // 生成用于计算签名的字符串 stringToSign |
175 | - $stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1)); |
|
175 | + $stringToSign = 'GET&%2F&'.$this->percentencode(substr($canonicalizedQueryString, 1)); |
|
176 | 176 | |
177 | 177 | // 计算签名,注意accessKeySecret后面要加上字符'&' |
178 | - $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true)); |
|
178 | + $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret.'&', true)); |
|
179 | 179 | return $signature; |
180 | 180 | } |
181 | 181 | |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
193 | 193 | } |
194 | 194 | //https 请求 |
195 | - if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
|
195 | + if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") { |
|
196 | 196 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
197 | 197 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
198 | 198 | } |
@@ -203,9 +203,9 @@ discard block |
||
203 | 203 | $postMultipart = false; |
204 | 204 | foreach ($postFields as $k => $v) |
205 | 205 | { |
206 | - if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
206 | + if ("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
207 | 207 | { |
208 | - $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
208 | + $postBodyString .= "$k=".urlencode($v)."&"; |
|
209 | 209 | } |
210 | 210 | else//文件上传用multipart/form-data,否则用www-form-urlencoded |
211 | 211 | { |
@@ -220,14 +220,14 @@ discard block |
||
220 | 220 | } |
221 | 221 | else |
222 | 222 | { |
223 | - curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
|
223 | + curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1)); |
|
224 | 224 | } |
225 | 225 | } |
226 | 226 | $reponse = curl_exec($ch); |
227 | 227 | |
228 | 228 | if (curl_errno($ch)) |
229 | 229 | { |
230 | - throw new Exception(curl_error($ch),0); |
|
230 | + throw new Exception(curl_error($ch), 0); |
|
231 | 231 | } |
232 | 232 | curl_close($ch); |
233 | 233 | return $reponse; |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | { |
238 | 238 | $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
239 | 239 | $logger = new LtLogger; |
240 | - $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->accessKeyId . "_" . date("Y-m-d") . ".log"; |
|
240 | + $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/').'/'."logs/top_comm_err_".$this->accessKeyId."_".date("Y-m-d").".log"; |
|
241 | 241 | $logger->conf["separator"] = "^_^"; |
242 | 242 | $logData = array( |
243 | 243 | date("Y-m-d H:i:s"), |
@@ -248,7 +248,7 @@ discard block |
||
248 | 248 | $this->sdkVersion, |
249 | 249 | $requestUrl, |
250 | 250 | $errorCode, |
251 | - str_replace("\n","",$responseTxt) |
|
251 | + str_replace("\n", "", $responseTxt) |
|
252 | 252 | ); |
253 | 253 | $logger->log($logData); |
254 | 254 | } |
@@ -64,15 +64,13 @@ discard block |
||
64 | 64 | try |
65 | 65 | { |
66 | 66 | $resp = $this->curl($requestUrl, null); |
67 | - } |
|
68 | - catch (Exception $e) |
|
67 | + } catch (Exception $e) |
|
69 | 68 | { |
70 | 69 | $this->logCommunicationError($apiParams["Action"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
71 | 70 | if ("json" == $this->format) |
72 | 71 | { |
73 | 72 | return json_decode($e->getMessage()); |
74 | - } |
|
75 | - else if("xml" == $this->format) |
|
73 | + } else if("xml" == $this->format) |
|
76 | 74 | { |
77 | 75 | return @simplexml_load_string($e->getMessage()); |
78 | 76 | } |
@@ -87,8 +85,7 @@ discard block |
||
87 | 85 | { |
88 | 86 | $respWellFormed = true; |
89 | 87 | } |
90 | - } |
|
91 | - else if("xml" == $this->format) |
|
88 | + } else if("xml" == $this->format) |
|
92 | 89 | { |
93 | 90 | $respObject = @simplexml_load_string($resp); |
94 | 91 | if (false !== $respObject) |
@@ -203,11 +200,12 @@ discard block |
||
203 | 200 | $postMultipart = false; |
204 | 201 | foreach ($postFields as $k => $v) |
205 | 202 | { |
206 | - if("@" != substr($v, 0, 1))//判断是不是文件上传 |
|
203 | + if("@" != substr($v, 0, 1)) { |
|
204 | + //判断是不是文件上传 |
|
207 | 205 | { |
208 | - $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
206 | + $postBodyString .= "$k=" . urlencode($v) . "&"; |
|
209 | 207 | } |
210 | - else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
208 | + } else//文件上传用multipart/form-data,否则用www-form-urlencoded |
|
211 | 209 | { |
212 | 210 | $postMultipart = true; |
213 | 211 | } |
@@ -217,8 +215,7 @@ discard block |
||
217 | 215 | if ($postMultipart) |
218 | 216 | { |
219 | 217 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
220 | - } |
|
221 | - else |
|
218 | + } else |
|
222 | 219 | { |
223 | 220 | curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
224 | 221 | } |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | class Autoloader{ |
4 | 4 | |
5 | - /** |
|
5 | + /** |
|
6 | 6 | * 类库自动加载,写死路径,确保不加载其他文件。 |
7 | 7 | * @param string $class 对象类名 |
8 | 8 | * @return void |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | public static function autoload($class) { |
11 | 11 | $name = $class; |
12 | 12 | if(false !== strpos($name,'\\')){ |
13 | - $name = strstr($class, '\\', true); |
|
13 | + $name = strstr($class, '\\', true); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | $filename = TOP_AUTOLOADER_PATH."/top/".$name.".php"; |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | return; |
67 | 67 | } |
68 | 68 | |
69 | - $filename = TOP_AUTOLOADER_PATH."/QimenCloud/".$name.".php"; |
|
69 | + $filename = TOP_AUTOLOADER_PATH."/QimenCloud/".$name.".php"; |
|
70 | 70 | if(is_file($filename)) { |
71 | 71 | include $filename; |
72 | 72 | return; |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -class Autoloader{ |
|
3 | +class Autoloader { |
|
4 | 4 | |
5 | 5 | /** |
6 | 6 | * 类库自动加载,写死路径,确保不加载其他文件。 |
@@ -9,65 +9,65 @@ discard block |
||
9 | 9 | */ |
10 | 10 | public static function autoload($class) { |
11 | 11 | $name = $class; |
12 | - if(false !== strpos($name,'\\')){ |
|
12 | + if (false !== strpos($name, '\\')) { |
|
13 | 13 | $name = strstr($class, '\\', true); |
14 | 14 | } |
15 | 15 | |
16 | 16 | $filename = TOP_AUTOLOADER_PATH."/top/".$name.".php"; |
17 | - if(is_file($filename)) { |
|
17 | + if (is_file($filename)) { |
|
18 | 18 | include $filename; |
19 | 19 | return; |
20 | 20 | } |
21 | 21 | |
22 | 22 | $filename = TOP_AUTOLOADER_PATH."/top/request/".$name.".php"; |
23 | - if(is_file($filename)) { |
|
23 | + if (is_file($filename)) { |
|
24 | 24 | include $filename; |
25 | 25 | return; |
26 | 26 | } |
27 | 27 | |
28 | 28 | $filename = TOP_AUTOLOADER_PATH."/top/domain/".$name.".php"; |
29 | - if(is_file($filename)) { |
|
29 | + if (is_file($filename)) { |
|
30 | 30 | include $filename; |
31 | 31 | return; |
32 | 32 | } |
33 | 33 | |
34 | 34 | $filename = TOP_AUTOLOADER_PATH."/aliyun/".$name.".php"; |
35 | - if(is_file($filename)) { |
|
35 | + if (is_file($filename)) { |
|
36 | 36 | include $filename; |
37 | 37 | return; |
38 | 38 | } |
39 | 39 | |
40 | 40 | $filename = TOP_AUTOLOADER_PATH."/aliyun/request/".$name.".php"; |
41 | - if(is_file($filename)) { |
|
41 | + if (is_file($filename)) { |
|
42 | 42 | include $filename; |
43 | 43 | return; |
44 | 44 | } |
45 | 45 | |
46 | 46 | $filename = TOP_AUTOLOADER_PATH."/aliyun/domain/".$name.".php"; |
47 | - if(is_file($filename)) { |
|
47 | + if (is_file($filename)) { |
|
48 | 48 | include $filename; |
49 | 49 | return; |
50 | 50 | } |
51 | 51 | |
52 | 52 | $filename = TOP_AUTOLOADER_PATH."/dingtalk/".$name.".php"; |
53 | - if(is_file($filename)) { |
|
53 | + if (is_file($filename)) { |
|
54 | 54 | include $filename; |
55 | 55 | return; |
56 | 56 | } |
57 | 57 | $filename = TOP_AUTOLOADER_PATH."/dingtalk/request/".$name.".php"; |
58 | - if(is_file($filename)) { |
|
58 | + if (is_file($filename)) { |
|
59 | 59 | include $filename; |
60 | 60 | return; |
61 | 61 | } |
62 | 62 | |
63 | 63 | $filename = TOP_AUTOLOADER_PATH."/dingtalk/domain/".$name.".php"; |
64 | - if(is_file($filename)) { |
|
64 | + if (is_file($filename)) { |
|
65 | 65 | include $filename; |
66 | 66 | return; |
67 | 67 | } |
68 | 68 | |
69 | 69 | $filename = TOP_AUTOLOADER_PATH."/QimenCloud/".$name.".php"; |
70 | - if(is_file($filename)) { |
|
70 | + if (is_file($filename)) { |
|
71 | 71 | include $filename; |
72 | 72 | return; |
73 | 73 | } |
@@ -2,7 +2,7 @@ |
||
2 | 2 | |
3 | 3 | namespace DtApp\ThinkLibrary\service\taobao; |
4 | 4 | |
5 | -require_once __DIR__ . '/bin/TopSdk.php'; |
|
5 | +require_once __DIR__.'/bin/TopSdk.php'; |
|
6 | 6 | |
7 | 7 | use DtApp\ThinkLibrary\Service; |
8 | 8 | use TbkScInvitecodeGetRequest; |
@@ -942,20 +942,20 @@ discard block |
||
942 | 942 | $sign = $this->createSign(); |
943 | 943 | //组织参数 |
944 | 944 | $strParam = $this->createStrParam(); |
945 | - $strParam .= 'sign=' . $sign; |
|
945 | + $strParam .= 'sign='.$sign; |
|
946 | 946 | //访问服务 |
947 | 947 | if ($this->protocol === 'http') { |
948 | 948 | if (empty($this->sandbox)) { |
949 | - $url = 'http://gw.api.taobao.com/router/rest?' . $strParam; |
|
950 | - } else { |
|
951 | - $url = 'http://gw.api.tbsandbox.com/router/rest?' . $strParam; |
|
949 | + $url = 'http://gw.api.taobao.com/router/rest?'.$strParam; |
|
950 | + }else { |
|
951 | + $url = 'http://gw.api.tbsandbox.com/router/rest?'.$strParam; |
|
952 | 952 | } |
953 | 953 | } |
954 | 954 | if ($this->protocol === 'https') { |
955 | 955 | if (empty($this->sandbox)) { |
956 | - $url = 'https://eco.taobao.com/router/rest?' . $strParam; |
|
957 | - } else { |
|
958 | - $url = 'https://gw.api.tbsandbox.com/router/rest?' . $strParam; |
|
956 | + $url = 'https://eco.taobao.com/router/rest?'.$strParam; |
|
957 | + }else { |
|
958 | + $url = 'https://gw.api.tbsandbox.com/router/rest?'.$strParam; |
|
959 | 959 | } |
960 | 960 | } |
961 | 961 | $result = file_get_contents($url); |
@@ -980,7 +980,7 @@ discard block |
||
980 | 980 | ksort($this->param); |
981 | 981 | foreach ($this->param as $key => $val) { |
982 | 982 | if ($key !== '' && $val !== '') { |
983 | - $sign .= $key . $val; |
|
983 | + $sign .= $key.$val; |
|
984 | 984 | } |
985 | 985 | } |
986 | 986 | $sign .= $this->app_secret; |
@@ -997,7 +997,7 @@ discard block |
||
997 | 997 | $strParam = ''; |
998 | 998 | foreach ($this->param as $key => $val) { |
999 | 999 | if ($key !== '' && $val !== '') { |
1000 | - $strParam .= $key . '=' . urlencode($val) . '&'; |
|
1000 | + $strParam .= $key.'='.urlencode($val).'&'; |
|
1001 | 1001 | } |
1002 | 1002 | } |
1003 | 1003 | return $strParam; |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * Pop response 类 |
6 | 6 | */ |
7 | 7 | |
8 | -class PopHttpResponse{ |
|
8 | +class PopHttpResponse { |
|
9 | 9 | /** |
10 | 10 | * @var int |
11 | 11 | */ |
@@ -79,8 +79,8 @@ discard block |
||
79 | 79 | */ |
80 | 80 | public function getContent() |
81 | 81 | { |
82 | - if($this->content === null){ |
|
83 | - $this->content = json_decode($this->getBody(),true); |
|
82 | + if ($this->content === null) { |
|
83 | + $this->content = json_decode($this->getBody(), true); |
|
84 | 84 | } |
85 | 85 | return $this->content; |
86 | 86 | } |