抖音小程序

HttpService.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Support\EmailQueue;
  5. class HttpService
  6. {
  7. /*
  8. * 封装HTTP请求 -- POST
  9. * */
  10. public static function httpPost($url, $data = null,$json = FALSE, $headers = null){
  11. //创建了一个curl会话资源,成功返回一个句柄;
  12. $curl = curl_init();
  13. //设置url
  14. curl_setopt($curl, CURLOPT_URL, $url);
  15. //设置为FALSE 禁止 cURL 验证对等证书(peer’s certificate)
  16. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  17. //设置为 1 是检查服务器SSL证书中是否存在一个公用名(common name)
  18. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  19. if (!empty($data)){
  20. //设置请求为POST
  21. curl_setopt($curl, CURLOPT_POST, 1);
  22. //curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); //最长的可忍受的连接时间
  23. //设置POST的数据域
  24. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  25. }
  26. if($json) {
  27. $header = array(
  28. 'Content-Type: application/json'
  29. );
  30. }
  31. if(!empty($headers)) {
  32. $header = array_merge($header, $headers);
  33. }
  34. curl_setopt($curl, CURLOPT_HEADER, 0);
  35. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  36. //设置是否将响应结果存入变量,1是存入,0是直接输出
  37. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  38. //然后将响应结果存入变量
  39. $output = curl_exec($curl);
  40. //关闭这个curl会话资源
  41. curl_close($curl);
  42. return $output;
  43. }
  44. /*
  45. * 封装HTTP请求 -- GET
  46. * */
  47. public static function httpGet($url, $headers = null){
  48. $curl = curl_init(); // 启动一个CURL会话
  49. curl_setopt($curl, CURLOPT_URL, $url);
  50. curl_setopt($curl, CURLOPT_HEADER, 0);
  51. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  52. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  53. //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
  54. if ($headers) {
  55. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 设置请求的header
  56. }
  57. $output = curl_exec($curl); //返回api的json对象
  58. //关闭URL请求
  59. curl_close($curl);
  60. return $output; //返回json对象
  61. }
  62. /*
  63. * 封装HTTP请求 -- 通过图片全路径上传图片
  64. * */
  65. public static function httpUpload($url, $filePath)
  66. {
  67. if(strstr($filePath, '../') !== false) {
  68. $filePath = public_path($filePath);
  69. }
  70. $file_data = array("media" => new \CURLFile($filePath));
  71. $curl = curl_init();
  72. curl_setopt($curl , CURLOPT_URL , $url);
  73. curl_setopt($curl , CURLOPT_RETURNTRANSFER, 1);
  74. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  75. curl_setopt($curl , CURLOPT_POST, 1);
  76. curl_setopt($curl , CURLOPT_POSTFIELDS, $file_data);
  77. $output = curl_exec($curl);
  78. if ($output === FALSE){
  79. # 增加报警
  80. EmailQueue::rPush('图片全路径上传失败', curl_error($curl), ['xiaohua.hou@kuxuan-inc.com'], '图片全路径上传失败');
  81. Log::logError('上传图片HTTP请求失败', [
  82. 'error' => curl_error($curl),
  83. 'url' => $url,
  84. 'path' => $filePath,
  85. ], 'HttpUploadError');
  86. }
  87. curl_close($curl);
  88. return $output;
  89. }
  90. }