You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

490 lines
14 KiB

  1. <?php
  2. /**
  3. * 接口调用
  4. * ============================================================================
  5. * * 版权所有 蜘蛛行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm CurlInterface.php
  13. * Create By 2016/11/10 13:20 $
  14. */
  15. namespace common\util;
  16. use backend\modules\api\util\Util;
  17. class CurlInterface
  18. {
  19. protected $url = null;
  20. protected $verb = 'GET';
  21. protected $body = '';
  22. protected $requestLength = 0;
  23. protected $baseUrl = null;
  24. protected $timeOut = 200;
  25. protected $clientId = 1;//站点类型 1 微信
  26. protected $responseBody = null;//接收到的返回值
  27. protected $notReturn = false; //是否有返回值
  28. protected $siteConfig = null; //配置参数
  29. protected $curlGetInfo = null;//交易概要
  30. private $logMessage = ''; //日志内容
  31. private $curlOptHeader = 0; //是否返回头部信息
  32. private $bodyType = 1;
  33. private $cert = false;//是否使用证书
  34. private $certPem;
  35. private $keyPem;
  36. private $header = [];//请求报文头部
  37. /****************============类的初始化=============*******************/
  38. /**
  39. * @param null $body 2:xml
  40. * @param int $type 1:json 2:xml 3 发送原数据接收xml 4 发送原数据 接收json 5:都是原数据
  41. */
  42. public function __construct($body = null, $type = 1)
  43. {
  44. $this->setBody($body, $type);
  45. $this->setBaseUrl();
  46. }
  47. /*****************==========参数设置函数开始=========******************/
  48. /**
  49. * Function Description:设置是否返回头部信息
  50. * Function Name: setCurlOptHeader
  51. * @param $curlOptHeader
  52. *
  53. *
  54. * @author 倪宗锋
  55. */
  56. public function setCurlOptHeader($curlOptHeader)
  57. {
  58. $this->curlOptHeader = $curlOptHeader;
  59. }
  60. /**
  61. * Function Description:获取交易概要
  62. * Function Name: getCurlGetInfo
  63. *
  64. * @return null
  65. *
  66. * @author 倪宗锋
  67. */
  68. public function getCurlGetInfo()
  69. {
  70. return $this->curlGetInfo;
  71. }
  72. /**
  73. * Des:设置 函数报文头
  74. * Name: setHeader
  75. * @param $header
  76. * @author 倪宗锋
  77. */
  78. public function setHeader($header)
  79. {
  80. $this->header[] = $header;
  81. }
  82. /**
  83. * Function Description:获取返回值报文
  84. * Function Name: getResponseBody
  85. *
  86. * @return null
  87. *
  88. * @author 倪宗锋
  89. */
  90. public function getResponseBody()
  91. {
  92. return $this->responseBody;
  93. }
  94. /**
  95. * Function Description:获取全路径地址
  96. * Function Name: getUrl
  97. *
  98. * @return null
  99. *
  100. * @author 倪宗锋
  101. */
  102. public function getUrl()
  103. {
  104. return $this->url;
  105. }
  106. /**
  107. * Function Description:设置路径 在baseUrl基础上
  108. * Function Name: setUrl
  109. * @param $url
  110. *
  111. *
  112. * @author 倪宗锋
  113. */
  114. public function setUrl($url)
  115. {
  116. $this->url = $this->baseUrl . $url;
  117. }
  118. /**
  119. * Function Description:设置过期时间
  120. * Function Name: setTimeOut
  121. * @param $timeOut
  122. *
  123. *
  124. * @author 倪宗锋
  125. */
  126. public function setTimeOut($timeOut)
  127. {
  128. $this->timeOut = $timeOut;
  129. }
  130. /**
  131. * Function Description:设置传值方式
  132. * Function Name: setVerb
  133. * @param $verb
  134. *
  135. *
  136. * @author 倪宗锋
  137. */
  138. public function setVerb($verb)
  139. {
  140. $this->verb = $verb;
  141. }
  142. /**
  143. * Function Description:设置baseUrl
  144. * Function Name: setBaseUrl
  145. * @param $baseUrl string
  146. *
  147. * @author 倪宗锋
  148. */
  149. public function setBaseUrl($baseUrl = '')
  150. {
  151. $this->baseUrl = $baseUrl;
  152. }
  153. /**
  154. * Function Description:获取BaseUrl
  155. * Function Name: getBaseUrl
  156. *
  157. * @return null
  158. *
  159. * @author 倪宗锋
  160. */
  161. public function getBaseUrl()
  162. {
  163. return $this->baseUrl;
  164. }
  165. /**
  166. * Function Description:设置是否有返回值
  167. * Function Name: setNotReturn
  168. * @param $str
  169. *
  170. *
  171. * @author 倪宗锋
  172. */
  173. public function setNotReturn($str)
  174. {
  175. $this->notReturn = $str;
  176. }
  177. /**
  178. * Function Description:设置请求报文 $body 必须是个数组
  179. * Function Name: setBody
  180. * @param $body
  181. * @param $type int 1:json 2:xml 3 发送原数据接收xml 4 发送原数据 接收json 5:都是原数据
  182. *
  183. * @author 倪宗锋
  184. */
  185. public function setBody($body, $type = 1)
  186. {
  187. $this->body = '';
  188. if (is_array($body)) {
  189. if ($type == 1) {
  190. $this->body = json_encode($body);
  191. $this->requestLength = strlen($this->body);
  192. } elseif ($type == 2) {
  193. $this->body = "<?xml version='1.0' encoding='UTF-8'?>";
  194. $this->body .= Util::arrayToXml($body);
  195. $this->requestLength = strlen($this->body);
  196. } elseif (in_array($type, array(3, 4, 5))) {
  197. $this->body = $body;
  198. }
  199. } else {
  200. $this->body = $body;
  201. }
  202. $this->bodyType = $type;
  203. }
  204. /**
  205. * Function Description:设置ssl类型
  206. * Function Name: setCert
  207. * @param $certArr array
  208. *
  209. *
  210. * @author 倪宗锋
  211. */
  212. public function setCert($certArr)
  213. {
  214. $this->cert = true;
  215. $this->certPem = $certArr['SSLCERT_PATH'];
  216. $this->keyPem = $certArr['SSLKEY_PATH'];
  217. }
  218. /*****************==========参数设置函数结束=========******************/
  219. /*****************==========调用接口并返回结果===开始======******************/
  220. /**
  221. * Function Description:执行交易
  222. * Function Name: execute
  223. * @param $url string sap地址
  224. * @param $verb string 请求方式 post|get
  225. * @return array
  226. * @author nizongfeng
  227. * Modify Date:2016.11.10
  228. */
  229. public function execute($url, $verb = 'GET')
  230. {
  231. $this->verb = $verb;
  232. $this->url = $this->baseUrl . $url;
  233. $this->logMessage .= date('Y-m-d H:i:s') . " Url : {$this->url}";
  234. $this->logMessage .= ' Method:' . $this->verb . PHP_EOL;
  235. if (is_array($this->body)) {
  236. $this->logMessage .= "sendContent: " . print_r($this->body, 1) . PHP_EOL;
  237. } else {
  238. $this->logMessage .= "sendContent: {$this->body}" . PHP_EOL;
  239. }
  240. $ch = curl_init($this->url);
  241. try {
  242. switch (strtoupper($this->verb)) {
  243. case 'GET':
  244. $this->executeGet($ch);
  245. break;
  246. case 'POST':
  247. $this->executePost($ch);
  248. break;
  249. case 'PUT':
  250. $this->executePut($ch);
  251. break;
  252. case 'DELETE':
  253. $this->executeDelete($ch);
  254. break;
  255. default:
  256. $this->logMessage .= "current verb: {$this->verb}, is an invalid REST verb." . PHP_EOL;
  257. break;
  258. }
  259. } catch (\Exception $e) {
  260. $this->logMessage .= $e->getMessage() . PHP_EOL;
  261. }
  262. curl_close($ch);
  263. $ch = null;
  264. return $this->getResult();
  265. }
  266. /**
  267. * Function Description:获取返回数据
  268. * Function Name: getResult
  269. *
  270. * @return array|mixed
  271. *
  272. * @author nizongfeng
  273. * Modify Date:2016.11.10
  274. */
  275. public function getResult()
  276. {
  277. if (in_array($this->bodyType, array(1, 4))) {
  278. $return = json_decode($this->responseBody, true);
  279. } elseif (in_array($this->bodyType, array(2, 3))) {
  280. $return = Util::xmlToArray($this->responseBody);
  281. } elseif ($this->bodyType == 5) {
  282. $return = $this->responseBody;
  283. } else {
  284. $return = '';
  285. }
  286. return $return;
  287. }
  288. /*******************=====GET传值=====********************/
  289. /**
  290. * Function Description:GET传值
  291. * Function Name: executeGet
  292. * @param $ch
  293. *
  294. * @return void
  295. *
  296. * @author nizongfeng 2016.11.10
  297. */
  298. protected function executeGet($ch)
  299. {
  300. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  301. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  302. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  303. $this->doExecute($ch);
  304. }
  305. /*******************=====POST传值=====********************/
  306. /**
  307. * Function Description:POST传值
  308. * Function Name: executePost
  309. * @param $ch
  310. *
  311. * @return void
  312. *
  313. * @author nizongfeng 2016.11.10
  314. */
  315. protected function executePost($ch)
  316. {
  317. // curl_setopt($ch, CURLOPT_VERBOSE, true);
  318. curl_setopt($ch, CURLOPT_POST, true);
  319. if (is_array($this->body)) {
  320. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->body));
  321. } else {
  322. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
  323. }
  324. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  325. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  326. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  327. $this->doExecute($ch);
  328. }
  329. /*******************=====PUT传值=====********************/
  330. /**
  331. * Function Description:PUT传值
  332. * Function Name: executePut
  333. * @param $ch
  334. *
  335. *
  336. * @author 倪宗锋
  337. */
  338. protected function executePut($ch)
  339. {
  340. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  341. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
  342. $this->doExecute($ch);
  343. }
  344. /*******************=====DELETE传值=====********************/
  345. /**
  346. * Function Description:DELETE传值
  347. * Function Name: executeDelete
  348. * @param $ch
  349. *
  350. *
  351. * @author 倪宗锋
  352. */
  353. protected function executeDelete($ch)
  354. {
  355. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
  356. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  357. $this->doExecute($ch);
  358. }
  359. /*******************=====传值及接收=====********************/
  360. /**
  361. * Function Description: 传值及接受数据
  362. * Function Name: doExecute
  363. * @param $curlHandle
  364. *
  365. * @return void
  366. *
  367. * @author nizongfeng 2015.12.08
  368. */
  369. protected function doExecute(&$curlHandle)
  370. {
  371. if ($this->verb != 'get') {
  372. $this->setCurlOpts($curlHandle);
  373. }
  374. if ($this->header != []) {
  375. curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $this->header);
  376. }
  377. curl_setopt($curlHandle, CURLOPT_HEADER, $this->curlOptHeader);
  378. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
  379. curl_setopt($curlHandle, CURLOPT_TIMEOUT, $this->timeOut);
  380. //记录报文发送时间
  381. $sendTime = microtime(true);
  382. $this->responseBody = curl_exec($curlHandle);
  383. //记录报文返回时间
  384. $responseTime = microtime(true);
  385. $timeIncrement = round(floatval($responseTime - $sendTime), 3);
  386. //记录返回的报文信息
  387. $this->logMessage .= "response: {$this->responseBody}" . PHP_EOL;
  388. $curlInfo = curl_getinfo($curlHandle);
  389. $this->curlGetInfo = $curlInfo;
  390. if (empty($curlInfo['primary_port'])) {
  391. $curlInfo['primary_port'] = '';
  392. }
  393. $curlInfoStr = '';
  394. if (isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_PORT']) {
  395. $curlInfoStr = " toIP {$curlInfo['primary_ip']}:{$curlInfo['primary_port']}";
  396. $curlInfoStr .= " selfIP {$_SERVER['SERVER_ADDR']} {$_SERVER['SERVER_PORT']}";
  397. }
  398. //记录通信信息及性能指标
  399. $this->logMessage .= "Info: " . json_encode($curlInfoStr) . PHP_EOL;
  400. $this->logMessage .= "sendTime: " . date('H:i:s', $sendTime) . " , responseTime: ";
  401. $this->logMessage .= date('H:i:s', $responseTime) . " , timeIncrement:" . $timeIncrement . 's'
  402. . PHP_EOL;
  403. $curlError = curl_error($curlHandle);
  404. if ($curlError) {
  405. $this->logMessage .= "Error: " . $curlError . PHP_EOL;
  406. }
  407. }
  408. /*******************=====头部设置=====********************/
  409. /**
  410. * Function Description:头部设置
  411. * Function Name: setCurlOpts
  412. * @param $curlHandle
  413. *
  414. *
  415. * @author 倪宗锋
  416. */
  417. protected function setCurlOpts(&$curlHandle)
  418. {
  419. if ($this->cert == 1) {
  420. //设置证书
  421. //使用证书:cert 与 key 分别属于两个.pem文件
  422. curl_setopt($curlHandle, CURLOPT_SSLCERTTYPE, 'PEM');
  423. curl_setopt($curlHandle, CURLOPT_SSLCERT, $this->certPem);
  424. curl_setopt($curlHandle, CURLOPT_SSLKEYTYPE, 'PEM');
  425. curl_setopt($curlHandle, CURLOPT_SSLKEY, $this->keyPem);
  426. }
  427. }
  428. /*****************==========调用接口并返回结果===结束======******************/
  429. //获取数组是几维数组
  430. private function getmaxdim($vDim)
  431. {
  432. if (!is_array($vDim)) return 0;
  433. else {
  434. $max1 = 0;
  435. foreach ($vDim as $item1) {
  436. $t1 = $this->getmaxdim($item1);
  437. if ($t1 > $max1) $max1 = $t1;
  438. }
  439. return $max1 + 1;
  440. }
  441. }
  442. public function __destruct()
  443. {
  444. if (defined('APP_MODULES_PATH')) {
  445. $file = APP_MODULES_PATH . '/runtime/log/curl/';
  446. } else {
  447. $file = APP_PATH . '/runtime/log/curl/';
  448. }
  449. if (file_exists($file) == false) {
  450. mkdir(iconv("UTF-8", "GBK", $file), 0777, true);
  451. }
  452. file_put_contents($file . date('Y-m-d') . '.log', $this->logMessage . PHP_EOL, FILE_APPEND);
  453. }
  454. }
  455. ?>