TopClient.php 10.0 KB

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