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.
 
 
 
 
 
 

466 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 addons\nzf;
  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. $url = preg_replace('# #','%20',$url);
  219. $this->verb = $verb;
  220. $this->url = $this->baseUrl . $url;
  221. $this->logMessage .= date('Y-m-d H:i:s') . " Url : {$this->url}";
  222. $this->logMessage .= ' Method:' . $this->verb . PHP_EOL;
  223. if (is_array($this->body)) {
  224. $this->logMessage .= "sendContent: " . json_encode($this->body) . PHP_EOL;
  225. } else {
  226. $this->logMessage .= "sendContent: {$this->body}" . PHP_EOL;
  227. }
  228. $ch = curl_init($this->url);
  229. try {
  230. switch (strtoupper($this->verb)) {
  231. case 'GET':
  232. $this->executeGet($ch);
  233. break;
  234. case 'POST':
  235. $this->executePost($ch);
  236. break;
  237. case 'PUT':
  238. $this->executePut($ch);
  239. break;
  240. case 'DELETE':
  241. $this->executeDelete($ch);
  242. break;
  243. default:
  244. $this->logMessage .= "current verb: {$this->verb}, is an invalid REST verb." . PHP_EOL;
  245. break;
  246. }
  247. } catch (\Exception $e) {
  248. $this->logMessage .= $e->getMessage() . PHP_EOL;
  249. }
  250. curl_close($ch);
  251. $ch = null;
  252. return $this->getResult();
  253. }
  254. /**
  255. * Function Description:获取返回数据
  256. * Function Name: getResult
  257. *
  258. * @return array|mixed
  259. *
  260. * @author nizongfeng
  261. * Modify Date:2016.11.10
  262. */
  263. public function getResult()
  264. {
  265. if (in_array($this->bodyType, array(1, 4))) {
  266. $return = json_decode($this->responseBody, true);
  267. } elseif (in_array($this->bodyType, array(2, 3))) {
  268. $return = Util::xmlToArray($this->responseBody);
  269. } elseif ($this->bodyType == 5) {
  270. $return = $this->responseBody;
  271. } else {
  272. $return = '';
  273. }
  274. // file_put_contents(APP_PATH . '/log/curl/' . date('Y-m-d') . '.log', $this->logMessage . PHP_EOL, FILE_APPEND);
  275. return $return;
  276. }
  277. /*******************=====GET传值=====********************/
  278. /**
  279. * Function Description:GET传值
  280. * Function Name: executeGet
  281. * @param $ch
  282. *
  283. * @return void
  284. *
  285. * @author nizongfeng 2016.11.10
  286. */
  287. protected function executeGet($ch)
  288. {
  289. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  290. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  291. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  292. $this->doExecute($ch);
  293. }
  294. /*******************=====POST传值=====********************/
  295. /**
  296. * Function Description:POST传值
  297. * Function Name: executePost
  298. * @param $ch
  299. *
  300. * @return void
  301. *
  302. * @author nizongfeng 2016.11.10
  303. */
  304. protected function executePost($ch)
  305. {
  306. // curl_setopt($ch, CURLOPT_VERBOSE, true);
  307. curl_setopt($ch, CURLOPT_POST, true);
  308. if (is_array($this->body)) {
  309. $cnt = $this->getmaxdim($this->body);
  310. if ($cnt > 1) {
  311. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->body));
  312. } else {
  313. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
  314. }
  315. } else {
  316. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
  317. }
  318. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  319. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  320. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  321. $this->doExecute($ch);
  322. }
  323. /*******************=====PUT传值=====********************/
  324. /**
  325. * Function Description:PUT传值
  326. * Function Name: executePut
  327. * @param $ch
  328. *
  329. *
  330. * @author 倪宗锋
  331. */
  332. protected function executePut($ch)
  333. {
  334. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  335. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
  336. $this->doExecute($ch);
  337. }
  338. /*******************=====DELETE传值=====********************/
  339. /**
  340. * Function Description:DELETE传值
  341. * Function Name: executeDelete
  342. * @param $ch
  343. *
  344. *
  345. * @author 倪宗锋
  346. */
  347. protected function executeDelete($ch)
  348. {
  349. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
  350. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  351. $this->doExecute($ch);
  352. }
  353. /*******************=====传值及接收=====********************/
  354. /**
  355. * Function Description: 传值及接受数据
  356. * Function Name: doExecute
  357. * @param $curlHandle
  358. *
  359. * @return void
  360. *
  361. * @author nizongfeng 2015.12.08
  362. */
  363. protected function doExecute(&$curlHandle)
  364. {
  365. if ($this->verb != 'get') {
  366. $this->setCurlOpts($curlHandle);
  367. }
  368. curl_setopt($curlHandle, CURLOPT_HEADER, $this->curlOptHeader);
  369. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
  370. curl_setopt($curlHandle, CURLOPT_TIMEOUT, $this->timeOut);
  371. //记录报文发送时间
  372. $sendTime = microtime(true);
  373. $this->responseBody = curl_exec($curlHandle);
  374. //记录报文返回时间
  375. $responseTime = microtime(true);
  376. $timeIncrement = round(floatval($responseTime - $sendTime), 3);
  377. //记录返回的报文信息
  378. $this->logMessage .= "response: {$this->responseBody}" . PHP_EOL;
  379. $curlInfo = curl_getinfo($curlHandle);
  380. $this->curlGetInfo = $curlInfo;
  381. if (empty($curlInfo['primary_port'])) {
  382. $curlInfo['primary_port'] = '';
  383. }
  384. $curlInfoStr = '';
  385. if (isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_PORT']) {
  386. $curlInfoStr = " toIP {$curlInfo['primary_ip']}:{$curlInfo['primary_port']}";
  387. $curlInfoStr .= " selfIP {$_SERVER['SERVER_ADDR']} {$_SERVER['SERVER_PORT']}";
  388. }
  389. //记录通信信息及性能指标
  390. $this->logMessage .= "Info: " . json_encode($curlInfoStr) . PHP_EOL;
  391. $this->logMessage .= "sendTime: " . date('H:i:s', $sendTime) . " , responseTime: ";
  392. $this->logMessage .= date('H:i:s', $responseTime) . " , timeIncrement:" . $timeIncrement . 's'
  393. . PHP_EOL;
  394. $curlError = curl_error($curlHandle);
  395. if ($curlError) {
  396. $this->logMessage .= "Error: " . $curlError . PHP_EOL;
  397. }
  398. }
  399. /*******************=====头部设置=====********************/
  400. /**
  401. * Function Description:头部设置
  402. * Function Name: setCurlOpts
  403. * @param $curlHandle
  404. *
  405. *
  406. * @author 倪宗锋
  407. */
  408. protected function setCurlOpts(&$curlHandle)
  409. {
  410. if ($this->cert == 1) {
  411. //设置证书
  412. //使用证书:cert 与 key 分别属于两个.pem文件
  413. curl_setopt($curlHandle, CURLOPT_SSLCERTTYPE, 'PEM');
  414. curl_setopt($curlHandle, CURLOPT_SSLCERT, $this->certPem);
  415. curl_setopt($curlHandle, CURLOPT_SSLKEYTYPE, 'PEM');
  416. curl_setopt($curlHandle, CURLOPT_SSLKEY, $this->keyPem);
  417. }
  418. }
  419. /*****************==========调用接口并返回结果===结束======******************/
  420. //获取数组是几维数组
  421. private function getmaxdim($vDim)
  422. {
  423. if (!is_array($vDim)) return 0;
  424. else {
  425. $max1 = 0;
  426. foreach ($vDim as $item1) {
  427. $t1 = $this->getmaxdim($item1);
  428. if ($t1 > $max1) $max1 = $t1;
  429. }
  430. return $max1 + 1;
  431. }
  432. }
  433. }
  434. ?>