10、CURL类封装

在对接API接口中,我们不可能每次都重新编写CURL系列代码,
肯定是会把CURL封装成一个自定义函数,
但由于CURL的环境参数多变,我们不可能设计一个传递很多参数的函数,然后中间嵌套数十个IF判断。
CURL的封装应该使用Class类,用成员属性可以控制参数,用于成员方法来进行逻辑解耦。

  1. <?php
  2. class Curl{
  3. /**
  4. * API请求地址
  5. */
  6. public $apiUrl;
  7. /**
  8. * API请求参数
  9. */
  10. public $apiData;
  11. /**
  12. * API请求头信息
  13. */
  14. public $apiHeaders;
  15. /**
  16. * HTTP动词
  17. */
  18. public $apiHavior = 'GET';
  19. /**
  20. * 请求最长等待时间(秒)
  21. */
  22. public $apiTime = 60;
  23. /**
  24. * 是否使用POST
  25. */
  26. public $apiPost = false;
  27. /**
  28. * 是否开启debug模式
  29. */
  30. public $apiDebug = false;
  31. /**
  32. * 是否开启debug模式
  33. */
  34. public $DebugLog = [];
  35. /**
  36. * CURL连接句柄
  37. */
  38. private $_curl;
  39. /**
  40. * 发送请求
  41. * @return
  42. */
  43. public function exec() {
  44. # 检测参数
  45. $this->issetName();
  46. # 初始化连接句柄
  47. $this->_curl = curl_init();
  48. # 设置基本请求信息
  49. $this->set_basicHead();
  50. # 设置重要的请求信息
  51. $this->set_vitalHead();
  52. if ($this->apiDebug == true) { $this->DebugLog[] = '开始发送请求...'; }
  53. # 发送CURL
  54. $res = curl_exec($this->_curl);
  55. if ($res === false) {
  56. if ($this->apiDebug == true) { $this->DebugLog[] = '请求失败...'; }
  57. return $this->Debug('API请求失败,原因:'.curl_error($this->_curl));
  58. }
  59. if ($this->apiDebug == true) { $this->DebugLog[] = '请求成功...out'; }
  60. # 释放curl句柄
  61. curl_close($this->_curl);
  62. # 返回请求结果
  63. return $res;
  64. }
  65. /**
  66. * 检测各项必填参数
  67. *
  68. * @return bool
  69. */
  70. private function issetName() {
  71. if ($this->apiDebug == true) { $this->DebugLog[] = '开始检测参数...'; }
  72. if (empty($this->apiUrl)) { return $this->Debug('API请求地址为空');}
  73. if (empty($this->apiHavior)) { return $this->Debug('API请求动词为空');}
  74. if ($this->apiDebug == true) { $this->DebugLog[] = '开始检测通过...OK'; }
  75. return true;
  76. }
  77. /**
  78. * 用于Debug下输出内容还是返回bool
  79. *
  80. * @param string $msg Debug模式下记录错误提示
  81. * @return bool false
  82. */
  83. private function Debug($msg=null) {
  84. if ($this->apiDebug == true) {
  85. if ($this->apiDebug == true) { $this->DebugLog[] = $msg; }
  86. }
  87. return false;
  88. }
  89. /**
  90. * 设置基本请求信息
  91. */
  92. private function set_basicHead() {
  93. if ($this->apiDebug == true) { $this->DebugLog[] = '设置基本请求参数...'; }
  94. # 设置API请求地址
  95. curl_setopt($this->_curl, CURLOPT_URL, $this->apiUrl);
  96. # 关闭请求成功后自动输出的设置
  97. curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true);
  98. # 关闭回调内容中带请求头信息
  99. curl_setopt($this->_curl, CURLOPT_HEADER, false);
  100. # 设置最大请求等待时间为60秒
  101. curl_setopt($this->_curl, CURLOPT_TIMEOUT, $this->apiTime);
  102. # 设置请求HTTP动词
  103. curl_setopt($this->_curl, CURLOPT_CUSTOMREQUEST, $this->apiHavior);
  104. # 设置请求类型为POST
  105. if ($this->apiPost) {
  106. curl_setopt($this->_curl, CURLOPT_POST, true);
  107. if ($this->apiDebug == true) { $this->DebugLog[] = '1、开启POST请求...OK'; }
  108. }
  109. # 自动检测https请求
  110. if (strripos($this->apiUrl, 'https') !== false) {
  111. curl_setopt($this->_curl, CURLOPT_SSL_VERIFYPEER, false);
  112. curl_setopt($this->_curl, CURLOPT_SSL_VERIFYSTATUS, false);
  113. if ($this->apiDebug == true) { $this->DebugLog[] = '2、开启HTTPS请求协议...OK'; }
  114. }
  115. if ($this->apiDebug == true) { $this->DebugLog[] = '基本请求参数设置完成...'; }
  116. }
  117. /**
  118. * 设置重要的请求信息
  119. */
  120. private function set_vitalHead() {
  121. if ($this->apiDebug == true) { $this->DebugLog[] = '设置重要请求参数...'; }
  122. # 设置请求参数
  123. if (!empty($this->apiData)) {
  124. curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $this->apiData);
  125. if ($this->apiDebug == true) { $this->DebugLog[] = '1、设置请求内容...OK'; }
  126. }
  127. # 设置请求头
  128. if (!empty($this->apiHeaders)) {
  129. curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $this->apiHeaders);
  130. if ($this->apiDebug == true) { $this->DebugLog[] = '2、设置请求头...OK'; }
  131. }
  132. if ($this->apiDebug == true) { $this->DebugLog[] = '重要请求参数设置完成...'; }
  133. }
  134. }
  135. # 使用demo
  136. $curl = new Curl();
  137. $curl->apiDebug = true; // 开启调试模式
  138. $curl->apiUrl = 'https://www.baidu.com/11';
  139. $res = $curl->exec();
  140. echo '<pre>';
  141. var_dump($res); // 打印返回值
  142. var_dump($curl->DebugLog); // 打印LOG