HttpCurl.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App\Http\helper;
  3. use App\AdminUser;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\DB;
  6. class HttpCurl
  7. {
  8. // 往中转服发通知
  9. public function service($url, $data)
  10. {
  11. $admin = DB::connection('write')->table('agent.dbo.admin_users')->where('id', 1)->select('account', 'password')->first();
  12. $account = $admin->account ?: '';
  13. $password = $admin->password ?: '';
  14. $data['accounts'] = $account;
  15. $data['passworld'] = $password;
  16. $build_query = $url . '?' . http_build_query($data);
  17. return HttpCurl::curl_get_shot($build_query);
  18. }
  19. function curl_getHeader($url, $header = [])
  20. {
  21. $headerArray = [];
  22. if (!empty($header)) {
  23. foreach ($header as $key => $value) {
  24. $headerArray[] = $key . ':' . $value;
  25. }
  26. }
  27. $ch = curl_init();
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  29. curl_setopt($ch, CURLOPT_TIMEOUT, 60); //只需要设置一个秒的数量就可以
  30. curl_setopt($ch, CURLOPT_URL, $url);
  31. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  32. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
  35. $output = curl_exec($ch);
  36. curl_close($ch);
  37. $output = mb_convert_encoding($output, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
  38. return $output;
  39. }
  40. function curl_get($url, $header = [])
  41. {
  42. // $headerArray = ['Content-type:application/json;", "Accept:application/json'];
  43. $headerArray = [
  44. 'Content-Type: application/json;charset=UTF-8',
  45. ];
  46. if (!empty($header)) {
  47. foreach ($header as $key => $value) {
  48. $headerArray[] = $key . ':' . $value;
  49. }
  50. }
  51. $ch = curl_init();
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  53. curl_setopt($ch, CURLOPT_TIMEOUT, 60); //只需要设置一个秒的数量就可以
  54. curl_setopt($ch, CURLOPT_URL, $url);
  55. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  58. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
  59. $output = curl_exec($ch);
  60. curl_close($ch);
  61. $output = mb_convert_encoding($output, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
  62. return $output;
  63. }
  64. static function curl_get_shot($url, $header = [])
  65. {
  66. // $headerArray = ['Content-type:application/json;", "Accept:application/json'];
  67. $headerArray = [
  68. 'Content-Type: application/json;charset=UTF-8',
  69. ];
  70. if (!empty($header)) {
  71. foreach ($header as $key => $value) {
  72. $headerArray[] = $key . ':' . $value;
  73. }
  74. }
  75. $ch = curl_init();
  76. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  77. curl_setopt($ch, CURLOPT_TIMEOUT, 5); //只需要设置一个秒的数量就可以
  78. curl_setopt($ch, CURLOPT_URL, $url);
  79. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  80. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  81. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  82. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
  83. $output = curl_exec($ch);
  84. curl_close($ch);
  85. $output = mb_convert_encoding($output, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
  86. return $output;
  87. }
  88. /**
  89. * 传入数组进行HTTP POST请求
  90. * @param $url
  91. * @param array $post_data
  92. * @param int $timeout
  93. * @param bool $header
  94. * @param string $data_type
  95. * @return bool|string
  96. */
  97. public function curlPost($url, $post_data = array(), $data_type = "array", $header = false, $timeout = 120, $flg = false)
  98. {
  99. //支持json数据数据提交
  100. if ($data_type == 'json') {
  101. $post_string = \GuzzleHttp\json_encode($post_data);
  102. } elseif ($data_type == 'array') {
  103. $post_string = $post_data;
  104. } elseif (is_array($post_data)) {
  105. $post_string = http_build_query($post_data, '', '&');
  106. }
  107. $ch = curl_init(); // 启动一个CURL会话
  108. curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
  109. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 // https请求 不验证证书和hosts
  110. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  111. curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
  112. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的数据包
  113. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
  114. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  115. //curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  116. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  117. if ($header) {
  118. $header = [
  119. 'Content-Type: application/json',
  120. ];
  121. curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
  122. }
  123. $result = curl_exec($ch);
  124. // 获取http code
  125. if ($flg) {
  126. return curl_getinfo($ch, CURLINFO_HTTP_CODE);
  127. }
  128. curl_close($ch);
  129. return $result;
  130. }
  131. /**
  132. * 传入数组进行HTTP POST 表单请求
  133. * @param $url
  134. * @param array $data
  135. * @return bool|string
  136. */
  137. public function curlPostForm($url, $data = array())
  138. {
  139. //定义提交内容类型
  140. $headers = array('Content-Type: application/x-www-form-urlencoded');
  141. $curl = curl_init(); // 启动一个CURL会话
  142. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  143. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  144. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
  145. //curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  146. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  147. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  148. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  149. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
  150. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  151. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  152. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  153. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  154. $result = curl_exec($curl); // 执行操作
  155. if (curl_errno($curl)) {
  156. Log::info('Errno'.curl_error($curl));
  157. }
  158. curl_close($curl); // 关闭CURL会话
  159. return $result;
  160. }
  161. // curl-header post
  162. function httpsPost($url, $paramStr,$headers){
  163. $curl = curl_init();
  164. curl_setopt_array($curl, array(
  165. CURLOPT_URL => $url,
  166. CURLOPT_RETURNTRANSFER => 1,
  167. CURLOPT_TIMEOUT => 120,
  168. CURLOPT_SSL_VERIFYPEER => false,
  169. CURLOPT_SSL_VERIFYHOST => false,
  170. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  171. CURLOPT_CUSTOMREQUEST => "POST",
  172. CURLOPT_POSTFIELDS => $paramStr,
  173. CURLOPT_HTTPHEADER => $headers,
  174. ));
  175. $response = curl_exec($curl);
  176. $err = curl_error($curl);
  177. curl_close($curl);
  178. if ($err) {
  179. return $err;
  180. }
  181. return $response;
  182. }
  183. }