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.
 
 
 
 
 
 

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