QimenCloudClient.php 11 KB

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