DingTalkClient.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. class DingTalkClient
  3. {
  4. /**@Author chaohui.zch copy from TopClient and modify 2016-12-14 **/
  5. /**@Author chaohui.zch modify $gatewayUrl 2017-07-18 **/
  6. public $gatewayUrl = "https://eco.taobao.com/router/rest";
  7. public $format = "xml";
  8. public $connectTimeout;
  9. public $readTimeout;
  10. public $apiCallType;
  11. public $httpMethod;
  12. /** 是否打开入参check**/
  13. public $checkRequest = true;
  14. protected $apiVersion = "2.0";
  15. protected $sdkVersion = "dingtalk-sdk-php-20161214";
  16. public function __construct($apiCallType = null, $httpMethod = null, $format = "xml"){
  17. $this->apiCallType = $apiCallType;
  18. $this->httpMethod = $httpMethod;
  19. $this->format = $format;
  20. }
  21. public function curl($url, $postFields = null)
  22. {
  23. $ch = curl_init();
  24. curl_setopt($ch, CURLOPT_URL, $url);
  25. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  26. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  27. if ($this->readTimeout) {
  28. curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
  29. }
  30. if ($this->connectTimeout) {
  31. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
  32. }
  33. curl_setopt ( $ch, CURLOPT_USERAGENT, "dingtalk-sdk-php" );
  34. //https 请求
  35. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  37. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  38. }
  39. if (is_array($postFields) && 0 < count($postFields))
  40. {
  41. $postBodyString = "";
  42. $postMultipart = false;
  43. foreach ($postFields as $k => $v)
  44. {
  45. if("@" != substr($v, 0, 1))//判断是不是文件上传
  46. {
  47. $postBodyString .= "$k=" . urlencode($v) . "&";
  48. }
  49. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  50. {
  51. $postMultipart = true;
  52. if(class_exists('\CURLFile')){
  53. $postFields[$k] = new \CURLFile(substr($v, 1));
  54. }
  55. }
  56. }
  57. unset($k, $v);
  58. curl_setopt($ch, CURLOPT_POST, true);
  59. if ($postMultipart)
  60. {
  61. if (class_exists('\CURLFile')) {
  62. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
  63. } else {
  64. if (defined('CURLOPT_SAFE_UPLOAD')) {
  65. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
  66. }
  67. }
  68. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  69. }
  70. else
  71. {
  72. $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");
  73. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  74. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  75. }
  76. }
  77. $reponse = curl_exec($ch);
  78. if (curl_errno($ch))
  79. {
  80. throw new Exception(curl_error($ch),0);
  81. }
  82. else
  83. {
  84. $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  85. if (200 !== $httpStatusCode)
  86. {
  87. throw new Exception($reponse,$httpStatusCode);
  88. }
  89. }
  90. curl_close($ch);
  91. return $reponse;
  92. }
  93. public function curl_get($url,$apiFields = null)
  94. {
  95. $ch = curl_init();
  96. foreach ($apiFields as $key => $value)
  97. {
  98. if(!is_string($value)){
  99. $value = json_encode($value);
  100. }
  101. $url .= "&" ."$key=" . urlencode($value);
  102. }
  103. curl_setopt($ch, CURLOPT_URL, $url);
  104. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  105. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  106. curl_setopt($ch, CURLOPT_HEADER, false);
  107. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  108. if ($this->readTimeout)
  109. {
  110. curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
  111. }
  112. if ($this->connectTimeout)
  113. {
  114. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
  115. }
  116. curl_setopt ( $ch, CURLOPT_USERAGENT, "dingtalk-sdk-php" );
  117. //https ignore ssl check ?
  118. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" )
  119. {
  120. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  121. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  122. }
  123. $reponse = curl_exec($ch);
  124. if (curl_errno($ch))
  125. {
  126. throw new Exception(curl_error($ch),0);
  127. }
  128. else
  129. {
  130. $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  131. if (200 !== $httpStatusCode)
  132. {
  133. throw new Exception($reponse,$httpStatusCode);
  134. }
  135. }
  136. curl_close($ch);
  137. return $reponse;
  138. }
  139. public function curl_json($url, $postFields = null)
  140. {
  141. $ch = curl_init();
  142. curl_setopt($ch, CURLOPT_URL, $url);
  143. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  144. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  145. if ($this->readTimeout) {
  146. curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
  147. }
  148. if ($this->connectTimeout) {
  149. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
  150. }
  151. curl_setopt ( $ch, CURLOPT_USERAGENT, "dingtalk-sdk-php" );
  152. //https 请求
  153. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  154. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  155. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  156. }
  157. if (is_array($postFields) && 0 < count($postFields))
  158. {
  159. $postBodyString = "";
  160. $postMultipart = false;
  161. foreach ($postFields as $k => $v)
  162. {
  163. if(!is_string($v)){
  164. $v = json_encode($v);
  165. }
  166. if("@" != substr($v, 0, 1))//判断是不是文件上传
  167. {
  168. $postBodyString .= "$k=" . urlencode($v) . "&";
  169. }
  170. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  171. {
  172. $postMultipart = true;
  173. if(class_exists('\CURLFile')){
  174. $postFields[$k] = new \CURLFile(substr($v, 1));
  175. }
  176. }
  177. }
  178. unset($k, $v);
  179. curl_setopt($ch, CURLOPT_POST, true);
  180. if ($postMultipart)
  181. {
  182. if (class_exists('\CURLFile')) {
  183. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
  184. } else {
  185. if (defined('CURLOPT_SAFE_UPLOAD')) {
  186. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
  187. }
  188. }
  189. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  190. }
  191. else {
  192. $header = array("Content-Type: application/json; charset=utf-8", "Content-Length:".strlen(json_encode($postFields)));
  193. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  194. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
  195. }
  196. }
  197. $reponse = curl_exec($ch);
  198. if (curl_errno($ch))
  199. {
  200. throw new Exception(curl_error($ch),0);
  201. }
  202. else
  203. {
  204. $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  205. if (200 !== $httpStatusCode)
  206. {
  207. throw new Exception($reponse,$httpStatusCode);
  208. }
  209. }
  210. curl_close($ch);
  211. return $reponse;
  212. }
  213. public function curl_with_memory_file($url, $postFields = null, $fileFields = null)
  214. {
  215. $ch = curl_init();
  216. curl_setopt($ch, CURLOPT_URL, $url);
  217. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  218. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  219. if ($this->readTimeout) {
  220. curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
  221. }
  222. if ($this->connectTimeout) {
  223. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
  224. }
  225. curl_setopt ( $ch, CURLOPT_USERAGENT, "dingtalk-sdk-php" );
  226. //https 请求
  227. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  228. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  229. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  230. }
  231. //生成分隔符
  232. $delimiter = '-------------' . uniqid();
  233. //先将post的普通数据生成主体字符串
  234. $data = '';
  235. if($postFields != null){
  236. foreach ($postFields as $name => $content) {
  237. $data .= "--" . $delimiter . "\r\n";
  238. $data .= 'Content-Disposition: form-data; name="' . $name . '"';
  239. //multipart/form-data 不需要urlencode,参见 http:stackoverflow.com/questions/6603928/should-i-url-encode-post-data
  240. $data .= "\r\n\r\n" . $content . "\r\n";
  241. }
  242. unset($name,$content);
  243. }
  244. //将上传的文件生成主体字符串
  245. if($fileFields != null){
  246. foreach ($fileFields as $name => $file) {
  247. $data .= "--" . $delimiter . "\r\n";
  248. $data .= 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $file['filename'] . "\" \r\n";
  249. $data .= 'Content-Type: ' . $file['type'] . "\r\n\r\n";//多了个文档类型
  250. $data .= $file['content'] . "\r\n";
  251. }
  252. unset($name,$file);
  253. }
  254. //主体结束的分隔符
  255. $data .= "--" . $delimiter . "--";
  256. curl_setopt($ch, CURLOPT_POST, true);
  257. curl_setopt($ch, CURLOPT_HTTPHEADER , array(
  258. 'Content-Type: multipart/form-data; boundary=' . $delimiter,
  259. 'Content-Length: ' . strlen($data))
  260. );
  261. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  262. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  263. $reponse = curl_exec($ch);
  264. unset($data);
  265. if (curl_errno($ch))
  266. {
  267. throw new Exception(curl_error($ch),0);
  268. }
  269. else
  270. {
  271. $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  272. if (200 !== $httpStatusCode)
  273. {
  274. throw new Exception($reponse,$httpStatusCode);
  275. }
  276. }
  277. curl_close($ch);
  278. return $reponse;
  279. }
  280. protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt)
  281. {
  282. $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI";
  283. $logger = new TopLogger;
  284. $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . "_" . date("Y-m-d") . ".log";
  285. $logger->conf["separator"] = "^_^";
  286. $logData = array(
  287. date("Y-m-d H:i:s"),
  288. $apiName,
  289. $localIp,
  290. PHP_OS,
  291. $this->sdkVersion,
  292. $requestUrl,
  293. $errorCode,
  294. str_replace("\n","",$responseTxt)
  295. );
  296. $logger->log($logData);
  297. }
  298. public function execute($request, $session = null,$bestUrl = null){
  299. if(DingTalkConstant::$CALL_TYPE_OAPI == $this->apiCallType){
  300. return $this->_executeOapi($request, $session, $bestUrl, null, null, null, null);
  301. }else{
  302. return $this->_execute($request, $session, $bestUrl);
  303. }
  304. }
  305. public function executeWithAccessKey($request, $bestUrl = null, $accessKey, $accessSecret){
  306. return $this->executeWithCorpId($request, $bestUrl, $accessKey, $accessSecret, null, null);
  307. }
  308. public function executeWithSuiteTicket($request,$bestUrl = null, $accessKey, $accessSecret, $suiteTicket){
  309. return $this->executeWithCorpId($request,$bestUrl, $accessKey, $accessSecret, $suiteTicket, null);
  310. }
  311. public function executeWithCorpId($request, $bestUrl = null, $accessKey, $accessSecret, $suiteTicket, $corpId) {
  312. if(DingTalkConstant::$CALL_TYPE_OAPI == $this->apiCallType){
  313. return $this->_executeOapi($request, null, $bestUrl,$accessKey, $accessSecret, $suiteTicket, $corpId);
  314. }else{
  315. return $this->_execute($request, null, $bestUrl);
  316. }
  317. }
  318. private function _executeOapi($request, $session = null,$bestUrl = null,$accessKey, $accessSecret, $suiteTicket, $corpId){
  319. $result = new ResultSet();
  320. if($this->checkRequest) {
  321. try {
  322. $request->check();
  323. } catch (Exception $e) {
  324. $result->code = $e->getCode();
  325. $result->msg = $e->getMessage();
  326. return $result;
  327. }
  328. }
  329. $sysParams["method"] = $request->getApiMethodName();
  330. //系统参数放入GET请求串
  331. if($bestUrl){
  332. if(strpos($bestUrl,'?') === false){
  333. $requestUrl = $bestUrl."?";
  334. }else{
  335. $requestUrl = $bestUrl;
  336. }
  337. }else{
  338. $requestUrl = $this->gatewayUrl."?";
  339. }
  340. if(null != $accessKey){
  341. $timestamp = $this->getMillisecond();
  342. // 验证签名有效性
  343. $canonicalString = $this->getCanonicalStringForIsv($timestamp, $suiteTicket);
  344. $signature = $this->computeSignature($accessSecret, $canonicalString);
  345. $queryParams["accessKey"] = $accessKey;
  346. $queryParams["signature"] = $signature;
  347. $queryParams["timestamp"] = $timestamp+"";
  348. if($suiteTicket != null) {
  349. $queryParams["suiteTicket"] = $suiteTicket;
  350. }
  351. if($corpId != null){
  352. $queryParams["corpId"] = $corpId;
  353. }
  354. foreach ($queryParams as $queryParamKey => $queryParamValue) {
  355. $requestUrl .= "$queryParamKey=" . urlencode($queryParamValue) . "&";
  356. }
  357. }else{
  358. $requestUrl .= "access_token=" . urlencode($session) . "&";
  359. }
  360. $apiParams = array();
  361. //获取业务参数
  362. $apiParams = $request->getApiParas();
  363. $fileFields = array();
  364. foreach ($apiParams as $key => $value) {
  365. if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){
  366. $value['name'] = $key;
  367. $fileFields[$key] = $value;
  368. unset($apiParams[$key]);
  369. }
  370. }
  371. // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&";
  372. $requestUrl = substr($requestUrl, 0, -1);
  373. //发起HTTP请求
  374. try
  375. {
  376. if(count($fileFields) > 0){
  377. $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields);
  378. }else{
  379. if(DingTalkConstant::$METHOD_POST == $this->httpMethod){
  380. $resp = $this->curl_json($requestUrl, $apiParams);
  381. }else{
  382. $resp = $this->curl_get($requestUrl, $apiParams);
  383. }
  384. }
  385. }
  386. catch (Exception $e)
  387. {
  388. $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage());
  389. $result->code = $e->getCode();
  390. $result->msg = $e->getMessage();
  391. return $result;
  392. }
  393. unset($apiParams);
  394. unset($fileFields);
  395. //解析TOP返回结果
  396. $respWellFormed = false;
  397. if ("json" == $this->format)
  398. {
  399. $respObject = json_decode($resp);
  400. if (null !== $respObject)
  401. {
  402. $respWellFormed = true;
  403. }
  404. }
  405. else if("xml" == $this->format)
  406. {
  407. $respObject = @simplexml_load_string($resp);
  408. if (false !== $respObject)
  409. {
  410. $respWellFormed = true;
  411. }
  412. }
  413. //返回的HTTP文本不是标准JSON或者XML,记下错误日志
  414. if (false === $respWellFormed)
  415. {
  416. $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp);
  417. $result->code = 0;
  418. $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED";
  419. return $result;
  420. }
  421. //如果TOP返回了错误码,记录到业务错误日志中
  422. if (isset($respObject->code))
  423. {
  424. $logger = new TopLogger;
  425. $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . "_" . date("Y-m-d") . ".log";
  426. $logger->log(array(
  427. date("Y-m-d H:i:s"),
  428. $resp
  429. ));
  430. }
  431. return $respObject;
  432. }
  433. private function getMillisecond() {
  434. list($s1, $s2) = explode(' ', microtime());
  435. return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
  436. }
  437. private function getCanonicalStringForIsv($timestamp, $suiteTicket) {
  438. $result = $timestamp;
  439. if($suiteTicket != null) {
  440. $result .= "\n".$suiteTicket;
  441. }
  442. return $result;
  443. }
  444. private function computeSignature($accessSecret, $canonicalString){
  445. $s = hash_hmac('sha256', $canonicalString, $accessSecret, true);
  446. return base64_encode($s);
  447. }
  448. private function _execute($request, $session = null,$bestUrl = null)
  449. {
  450. $result = new ResultSet();
  451. if($this->checkRequest) {
  452. try {
  453. $request->check();
  454. } catch (Exception $e) {
  455. $result->code = $e->getCode();
  456. $result->msg = $e->getMessage();
  457. return $result;
  458. }
  459. }
  460. //组装系统参数
  461. $sysParams["v"] = $this->apiVersion;
  462. $sysParams["format"] = $this->format;
  463. $sysParams["method"] = $request->getApiMethodName();
  464. $sysParams["timestamp"] = date("Y-m-d H:i:s");
  465. if (null != $session)
  466. {
  467. $sysParams["session"] = $session;
  468. }
  469. $apiParams = array();
  470. //获取业务参数
  471. $apiParams = $request->getApiParas();
  472. //系统参数放入GET请求串
  473. if($bestUrl){
  474. if(strpos($bestUrl,'?') === false){
  475. $requestUrl = $bestUrl."?";
  476. }else{
  477. $requestUrl = $bestUrl;
  478. }
  479. $sysParams["partner_id"] = $this->getClusterTag();
  480. }else{
  481. $requestUrl = $this->gatewayUrl."?";
  482. $sysParams["partner_id"] = $this->sdkVersion;
  483. }
  484. foreach ($sysParams as $sysParamKey => $sysParamValue)
  485. {
  486. // if(strcmp($sysParamKey,"timestamp") != 0)
  487. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  488. }
  489. $fileFields = array();
  490. foreach ($apiParams as $key => $value) {
  491. if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){
  492. $value['name'] = $key;
  493. $fileFields[$key] = $value;
  494. unset($apiParams[$key]);
  495. }
  496. }
  497. // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&";
  498. $requestUrl = substr($requestUrl, 0, -1);
  499. //发起HTTP请求
  500. try
  501. {
  502. if(count($fileFields) > 0){
  503. $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields);
  504. }else{
  505. $resp = $this->curl($requestUrl, $apiParams);
  506. }
  507. }
  508. catch (Exception $e)
  509. {
  510. $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage());
  511. $result->code = $e->getCode();
  512. $result->msg = $e->getMessage();
  513. return $result;
  514. }
  515. unset($apiParams);
  516. unset($fileFields);
  517. //解析TOP返回结果
  518. $respWellFormed = false;
  519. if ("json" == $this->format)
  520. {
  521. $respObject = json_decode($resp);
  522. if (null !== $respObject)
  523. {
  524. $respWellFormed = true;
  525. foreach ($respObject as $propKey => $propValue)
  526. {
  527. $respObject = $propValue;
  528. }
  529. }
  530. }
  531. else if("xml" == $this->format)
  532. {
  533. $respObject = @simplexml_load_string($resp);
  534. if (false !== $respObject)
  535. {
  536. $respWellFormed = true;
  537. }
  538. }
  539. //返回的HTTP文本不是标准JSON或者XML,记下错误日志
  540. if (false === $respWellFormed)
  541. {
  542. $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp);
  543. $result->code = 0;
  544. $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED";
  545. return $result;
  546. }
  547. //如果TOP返回了错误码,记录到业务错误日志中
  548. if (isset($respObject->code))
  549. {
  550. $logger = new TopLogger;
  551. $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . "_" . date("Y-m-d") . ".log";
  552. $logger->log(array(
  553. date("Y-m-d H:i:s"),
  554. $resp
  555. ));
  556. }
  557. return $respObject;
  558. }
  559. public function exec($paramsArray)
  560. {
  561. if (!isset($paramsArray["method"]))
  562. {
  563. trigger_error("No api name passed");
  564. }
  565. $inflector = new LtInflector;
  566. $inflector->conf["separator"] = ".";
  567. $requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request";
  568. if (!class_exists($requestClassName))
  569. {
  570. trigger_error("No such dingtalk-api: " . $paramsArray["method"]);
  571. }
  572. $session = isset($paramsArray["session"]) ? $paramsArray["session"] : null;
  573. $req = new $requestClassName;
  574. foreach($paramsArray as $paraKey => $paraValue)
  575. {
  576. $inflector->conf["separator"] = "_";
  577. $setterMethodName = $inflector->camelize($paraKey);
  578. $inflector->conf["separator"] = ".";
  579. $setterMethodName = "set" . $inflector->camelize($setterMethodName);
  580. if (method_exists($req, $setterMethodName))
  581. {
  582. $req->$setterMethodName($paraValue);
  583. }
  584. }
  585. return $this->execute($req, $session);
  586. }
  587. private function getClusterTag()
  588. {
  589. return substr($this->sdkVersion,0,11)."-cluster".substr($this->sdkVersion,11);
  590. }
  591. }