DingTalkClient.php 20 KB

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