TopClient.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. class TopClient
  3. {
  4. public $appkey;
  5. public $secretKey;
  6. public $gatewayUrl = "http://gw.api.taobao.com/router/rest";
  7. public $format = "xml";
  8. public $connectTimeout;
  9. public $readTimeout;
  10. /** 是否打开入参check**/
  11. public $checkRequest = true;
  12. protected $signMethod = "md5";
  13. protected $apiVersion = "2.0";
  14. protected $sdkVersion = "top-sdk-php-20180326";
  15. public function getAppkey()
  16. {
  17. return $this->appkey;
  18. }
  19. public function __construct($appkey = "",$secretKey = ""){
  20. $this->appkey = $appkey;
  21. $this->secretKey = $secretKey ;
  22. }
  23. protected function generateSign($params)
  24. {
  25. ksort($params);
  26. $stringToBeSigned = $this->secretKey;
  27. foreach ($params as $k => $v)
  28. {
  29. if(!is_array($v) && "@" != substr($v, 0, 1))
  30. {
  31. $stringToBeSigned .= "$k$v";
  32. }
  33. }
  34. unset($k, $v);
  35. $stringToBeSigned .= $this->secretKey;
  36. return strtoupper(md5($stringToBeSigned));
  37. }
  38. public function curl($url, $postFields = null)
  39. {
  40. $ch = curl_init();
  41. curl_setopt($ch, CURLOPT_URL, $url);
  42. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44. if ($this->readTimeout) {
  45. curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
  46. }
  47. if ($this->connectTimeout) {
  48. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
  49. }
  50. curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" );
  51. //https 请求
  52. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  53. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  54. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  55. }
  56. if (is_array($postFields) && 0 < count($postFields))
  57. {
  58. $postBodyString = "";
  59. $postMultipart = false;
  60. foreach ($postFields as $k => $v)
  61. {
  62. if("@" != substr($v, 0, 1))//判断是不是文件上传
  63. {
  64. $postBodyString .= "$k=" . urlencode($v) . "&";
  65. }
  66. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  67. {
  68. $postMultipart = true;
  69. if(class_exists('\CURLFile')){
  70. $postFields[$k] = new \CURLFile(substr($v, 1));
  71. }
  72. }
  73. }
  74. unset($k, $v);
  75. curl_setopt($ch, CURLOPT_POST, true);
  76. if ($postMultipart)
  77. {
  78. if (class_exists('\CURLFile')) {
  79. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
  80. } else {
  81. if (defined('CURLOPT_SAFE_UPLOAD')) {
  82. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
  83. }
  84. }
  85. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  86. }
  87. else
  88. {
  89. $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");
  90. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  91. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  92. }
  93. }
  94. $reponse = curl_exec($ch);
  95. if (curl_errno($ch))
  96. {
  97. throw new Exception(curl_error($ch),0);
  98. }
  99. else
  100. {
  101. $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  102. if (200 !== $httpStatusCode)
  103. {
  104. throw new Exception($reponse,$httpStatusCode);
  105. }
  106. }
  107. curl_close($ch);
  108. return $reponse;
  109. }
  110. public function curl_with_memory_file($url, $postFields = null, $fileFields = null)
  111. {
  112. $ch = curl_init();
  113. curl_setopt($ch, CURLOPT_URL, $url);
  114. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  115. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  116. if ($this->readTimeout) {
  117. curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
  118. }
  119. if ($this->connectTimeout) {
  120. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
  121. }
  122. curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" );
  123. //https 请求
  124. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  125. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  126. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  127. }
  128. //生成分隔符
  129. $delimiter = '-------------' . uniqid();
  130. //先将post的普通数据生成主体字符串
  131. $data = '';
  132. if($postFields != null){
  133. foreach ($postFields as $name => $content) {
  134. $data .= "--" . $delimiter . "\r\n";
  135. $data .= 'Content-Disposition: form-data; name="' . $name . '"';
  136. //multipart/form-data 不需要urlencode,参见 http:stackoverflow.com/questions/6603928/should-i-url-encode-post-data
  137. $data .= "\r\n\r\n" . $content . "\r\n";
  138. }
  139. unset($name,$content);
  140. }
  141. //将上传的文件生成主体字符串
  142. if($fileFields != null){
  143. foreach ($fileFields as $name => $file) {
  144. $data .= "--" . $delimiter . "\r\n";
  145. $data .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $file['name'] . "\" \r\n";
  146. $data .= 'Content-Type: ' . $file['type'] . "\r\n\r\n";//多了个文档类型
  147. $data .= $file['content'] . "\r\n";
  148. }
  149. unset($name,$file);
  150. }
  151. //主体结束的分隔符
  152. $data .= "--" . $delimiter . "--";
  153. curl_setopt($ch, CURLOPT_POST, true);
  154. curl_setopt($ch, CURLOPT_HTTPHEADER , array(
  155. 'Content-Type: multipart/form-data; boundary=' . $delimiter,
  156. 'Content-Length: ' . strlen($data))
  157. );
  158. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  159. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  160. $reponse = curl_exec($ch);
  161. unset($data);
  162. if (curl_errno($ch))
  163. {
  164. throw new Exception(curl_error($ch),0);
  165. }
  166. else
  167. {
  168. $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  169. if (200 !== $httpStatusCode)
  170. {
  171. throw new Exception($reponse,$httpStatusCode);
  172. }
  173. }
  174. curl_close($ch);
  175. return $reponse;
  176. }
  177. protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt)
  178. {
  179. $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI";
  180. $logger = new TopLogger;
  181. $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->appkey . "_" . date("Y-m-d") . ".log";
  182. $logger->conf["separator"] = "^_^";
  183. $logData = array(
  184. date("Y-m-d H:i:s"),
  185. $apiName,
  186. $this->appkey,
  187. $localIp,
  188. PHP_OS,
  189. $this->sdkVersion,
  190. $requestUrl,
  191. $errorCode,
  192. str_replace("\n","",$responseTxt)
  193. );
  194. $logger->log($logData);
  195. }
  196. public function execute($request, $session = null,$bestUrl = null)
  197. {
  198. $result = new ResultSet();
  199. if($this->checkRequest) {
  200. try {
  201. $request->check();
  202. } catch (Exception $e) {
  203. $result->code = $e->getCode();
  204. $result->msg = $e->getMessage();
  205. return $result;
  206. }
  207. }
  208. //组装系统参数
  209. $sysParams["app_key"] = $this->appkey;
  210. $sysParams["v"] = $this->apiVersion;
  211. $sysParams["format"] = $this->format;
  212. $sysParams["sign_method"] = $this->signMethod;
  213. $sysParams["method"] = $request->getApiMethodName();
  214. $sysParams["timestamp"] = date("Y-m-d H:i:s");
  215. if (null != $session)
  216. {
  217. $sysParams["session"] = $session;
  218. }
  219. $apiParams = array();
  220. //获取业务参数
  221. $apiParams = $request->getApiParas();
  222. //系统参数放入GET请求串
  223. if($bestUrl){
  224. $requestUrl = $bestUrl."?";
  225. $sysParams["partner_id"] = $this->getClusterTag();
  226. }else{
  227. $requestUrl = $this->gatewayUrl."?";
  228. $sysParams["partner_id"] = $this->sdkVersion;
  229. }
  230. //签名
  231. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  232. foreach ($sysParams as $sysParamKey => $sysParamValue)
  233. {
  234. // if(strcmp($sysParamKey,"timestamp") != 0)
  235. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  236. }
  237. $fileFields = array();
  238. foreach ($apiParams as $key => $value) {
  239. if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){
  240. $value['name'] = $key;
  241. $fileFields[$key] = $value;
  242. unset($apiParams[$key]);
  243. }
  244. }
  245. // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&";
  246. $requestUrl = substr($requestUrl, 0, -1);
  247. //发起HTTP请求
  248. try
  249. {
  250. if(count($fileFields) > 0){
  251. $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields);
  252. }else{
  253. $resp = $this->curl($requestUrl, $apiParams);
  254. }
  255. }
  256. catch (Exception $e)
  257. {
  258. $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage());
  259. $result->code = $e->getCode();
  260. $result->msg = $e->getMessage();
  261. return $result;
  262. }
  263. unset($apiParams);
  264. unset($fileFields);
  265. //解析TOP返回结果
  266. $respWellFormed = false;
  267. if ("json" == $this->format)
  268. {
  269. $respObject = json_decode($resp);
  270. if (null !== $respObject)
  271. {
  272. $respWellFormed = true;
  273. foreach ($respObject as $propKey => $propValue)
  274. {
  275. $respObject = $propValue;
  276. }
  277. }
  278. }
  279. else if("xml" == $this->format)
  280. {
  281. $respObject = @simplexml_load_string($resp);
  282. if (false !== $respObject)
  283. {
  284. $respWellFormed = true;
  285. }
  286. }
  287. //返回的HTTP文本不是标准JSON或者XML,记下错误日志
  288. if (false === $respWellFormed)
  289. {
  290. $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp);
  291. $result->code = 0;
  292. $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED";
  293. return $result;
  294. }
  295. //如果TOP返回了错误码,记录到业务错误日志中
  296. if (isset($respObject->code))
  297. {
  298. $logger = new TopLogger;
  299. $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log";
  300. $logger->log(array(
  301. date("Y-m-d H:i:s"),
  302. $resp
  303. ));
  304. }
  305. return $respObject;
  306. }
  307. public function exec($paramsArray)
  308. {
  309. if (!isset($paramsArray["method"]))
  310. {
  311. trigger_error("No api name passed");
  312. }
  313. $inflector = new LtInflector;
  314. $inflector->conf["separator"] = ".";
  315. $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request";
  316. if (!class_exists($requestClassName))
  317. {
  318. trigger_error("No such api: " . $paramsArray["method"]);
  319. }
  320. $session = isset($paramsArray["session"]) ? $paramsArray["session"] : null;
  321. $req = new $requestClassName;
  322. foreach($paramsArray as $paraKey => $paraValue)
  323. {
  324. $inflector->conf["separator"] = "_";
  325. $setterMethodName = $inflector->camelize($paraKey);
  326. $inflector->conf["separator"] = ".";
  327. $setterMethodName = "set" . $inflector->camelize($setterMethodName);
  328. if (method_exists($req, $setterMethodName))
  329. {
  330. $req->$setterMethodName($paraValue);
  331. }
  332. }
  333. return $this->execute($req, $session);
  334. }
  335. private function getClusterTag()
  336. {
  337. return substr($this->sdkVersion,0,11)."-cluster".substr($this->sdkVersion,11);
  338. }
  339. }