wangbiaoyan 6 ماه پیش
والد
کامیت
d07c0ae57f
2فایلهای تغییر یافته به همراه83 افزوده شده و 73 حذف شده
  1. 66 58
      src/HttpCurl.php
  2. 17 15
      src/Mq/MqService.php

+ 66 - 58
src/HttpCurl.php

@@ -29,30 +29,32 @@ class HttpCurl
      * @param array $params 请求参数
      * @param array $headers 请求头
      */
-    public static function getCurl(string $url,array $params=[],array $headers=[],$ssl=false)
+    public static function getCurl(string $url, array $params = [], array $headers = [], $ssl = false)
     {
         //判断是否存在traceId
-        if(isset($_GET["traceId"])){
+        if (isset($_GET["traceId"])) {
             $headers["traceId"] = $_GET["traceId"];
         }
 
         self::$curl = $curl = new Curl();
         //设置超时时间
-        $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
+        $curl->setOpt(CURLOPT_TIMEOUT, self::$timeOut);
         //设置连接超时时间
-        $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
+        $curl->setOpt(CURLOPT_CONNECTTIMEOUT, self::$connectTimeOut);
 
         //塞入header头
-        foreach ($headers as $key=>$header){
-            $curl->setHeader($key,$header);
+        foreach ($headers as $key => $header) {
+            $curl->setHeader($key, $header);
         }
-        if(!$ssl){
-            $curl->setOpt(CURLOPT_SSL_VERIFYPEER,false);
-            $curl->setOpt(CURLOPT_SSL_VERIFYHOST,false);
+        if (!$ssl) {
+            $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
+            $curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
         }
         //塞入参数
-        $curl->get($url,$params);
-        return json_decode($curl->response,true);
+        $curl->get($url, $params);
+        $res = json_decode($curl->response, true);
+        $curl->close();
+        return $res;
     }
 
 
@@ -62,34 +64,36 @@ class HttpCurl
      * @param array $headers 请求头
      * @param bool $asJson 是否json传输(true是 false否)
      */
-    public static function postCurl($url,$params=[],$headers=[],$asJson=true,$ssl=false)
+    public static function postCurl($url, $params = [], $headers = [], $asJson = true, $ssl = false)
     {
         //判断是否存在traceId
-        if(isset($_GET["traceId"])){
+        if (isset($_GET["traceId"])) {
             $headers["traceId"] = $_GET["traceId"];
         }
 
         self::$curl = $curl = new Curl();
         //设置超时时间
-        $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
+        $curl->setOpt(CURLOPT_TIMEOUT, self::$timeOut);
         //设置连接超时时间
-        $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
+        $curl->setOpt(CURLOPT_CONNECTTIMEOUT, self::$connectTimeOut);
 
         //塞入header头
-        foreach ($headers as $key=>$header){
-            $curl->setHeader($key,$header);
+        foreach ($headers as $key => $header) {
+            $curl->setHeader($key, $header);
         }
         //判断是否json请求
-        if($asJson){
-            $curl->setHeader("Content-Type","application/json;charset=utf-8");
+        if ($asJson) {
+            $curl->setHeader("Content-Type", "application/json;charset=utf-8");
         }
-        if(!$ssl){
-            $curl->setOpt(CURLOPT_SSL_VERIFYPEER,false);
-            $curl->setOpt(CURLOPT_SSL_VERIFYHOST,false);
+        if (!$ssl) {
+            $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
+            $curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
         }
         //塞入参数
-        $curl->post($url,$params,$asJson);
-        return json_decode($curl->response,true);
+        $curl->post($url, $params, $asJson);
+        $res = json_decode($curl->response, true);
+        $curl->close();
+        return $res;
     }
 
     /**
@@ -97,30 +101,32 @@ class HttpCurl
      * @param array $params 请求参数
      * @param array $headers 请求头
      */
-    public static function getCurlOrigin(string $url,array $params=[],array $headers=[],$ssl=false)
+    public static function getCurlOrigin(string $url, array $params = [], array $headers = [], $ssl = false)
     {
         //判断是否存在traceId
-        if(isset($_GET["traceId"])){
+        if (isset($_GET["traceId"])) {
             $headers["traceId"] = $_GET["traceId"];
         }
 
         self::$curl = $curl = new Curl();
         //设置超时时间
-        $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
+        $curl->setOpt(CURLOPT_TIMEOUT, self::$timeOut);
         //设置连接超时时间
-        $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
+        $curl->setOpt(CURLOPT_CONNECTTIMEOUT, self::$connectTimeOut);
 
         //塞入header头
-        foreach ($headers as $key=>$header){
-            $curl->setHeader($key,$header);
+        foreach ($headers as $key => $header) {
+            $curl->setHeader($key, $header);
         }
-        if(!$ssl){
-            $curl->setOpt(CURLOPT_SSL_VERIFYPEER,false);
-            $curl->setOpt(CURLOPT_SSL_VERIFYHOST,false);
+        if (!$ssl) {
+            $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
+            $curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
         }
         //塞入参数
-        $curl->get($url,$params);
-        return $curl->response;
+        $curl->get($url, $params);
+        $res = $curl->response;
+        $curl->close();
+        return $res;
     }
 
 
@@ -130,34 +136,36 @@ class HttpCurl
      * @param array $headers 请求头
      * @param bool $asJson 是否json传输(true是 false否)
      */
-    public static function postCurlOrigin($url,$params=[],$headers=[],$asJson=true,$ssl=false)
+    public static function postCurlOrigin($url, $params = [], $headers = [], $asJson = true, $ssl = false)
     {
         //判断是否存在traceId
-        if(isset($_GET["traceId"])){
+        if (isset($_GET["traceId"])) {
             $headers["traceId"] = $_GET["traceId"];
         }
 
         self::$curl = $curl = new Curl();
         //设置超时时间
-        $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
+        $curl->setOpt(CURLOPT_TIMEOUT, self::$timeOut);
         //设置连接超时时间
-        $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
+        $curl->setOpt(CURLOPT_CONNECTTIMEOUT, self::$connectTimeOut);
 
         //塞入header头
-        foreach ($headers as $key=>$header){
-            $curl->setHeader($key,$header);
+        foreach ($headers as $key => $header) {
+            $curl->setHeader($key, $header);
         }
         //判断是否json请求
-        if($asJson){
-            $curl->setHeader("Content-Type","application/json;charset=utf-8");
+        if ($asJson) {
+            $curl->setHeader("Content-Type", "application/json;charset=utf-8");
         }
-        if(!$ssl){
-            $curl->setOpt(CURLOPT_SSL_VERIFYPEER,false);
-            $curl->setOpt(CURLOPT_SSL_VERIFYHOST,false);
+        if (!$ssl) {
+            $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
+            $curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
         }
         //塞入参数
-        $curl->post($url,$params,$asJson);
-        return $curl->response;
+        $curl->post($url, $params, $asJson);
+        $res = $curl->response;
+        $curl->close();
+        return $res;
     }
 
     /**
@@ -165,27 +173,27 @@ class HttpCurl
      * @param string $url
      * @param string $filepath
      */
-    public static function uploadFile(string $url,string $filepath)
+    public static function uploadFile(string $url, string $filepath)
     {
 
-        $file = new \CURLFile($filepath,"application/octet-stream");
+        $file = new \CURLFile($filepath, "application/octet-stream");
         $data = [
-            "media"=>$file
+            "media" => $file
         ];
         $ch = curl_init($url);
-        curl_setopt($ch,CURLOPT_POST,1);
-        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
-        curl_setopt($ch,CURLOPT_HTTPHEADER,["multipart/form-data"]);
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, [ "multipart/form-data" ]);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-        curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
+        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
         $result = curl_exec($ch);
-        if(curl_errno($ch)){
+        if (curl_errno($ch)) {
             curl_close($ch);
             throw new HttpCustomeException("文件上传失败");
         }
         curl_close($ch);
-        $res = json_decode($result,1);
-        if($res["errcode"]==0){
+        $res = json_decode($result, 1);
+        if ($res["errcode"] == 0) {
             return $res["media_id"];
         }
     }

+ 17 - 15
src/Mq/MqService.php

@@ -13,6 +13,7 @@ use PhpAmqpLib\Message\AMQPMessage;
  */
 class MqService
 {
+
     /**
      * @var string 地址
      */
@@ -35,30 +36,31 @@ class MqService
 
     /**
      * 设置mq连接属性
-     * @param string $host  连接地址
-     * @param string $port  连接端口
+     * @param string $host 连接地址
+     * @param string $port 连接端口
      * @param string $userName 账号
      * @param string $userPassword 密码
      */
     public static function setMqAttributes($host, $port, $userName, $userPassword)
     {
-        self::$host=$host;
-        self::$port=$port;
-        self::$userName=$userName;
-        self::$userPassword=$userPassword;
+        self::$host = $host;
+        self::$port = $port;
+        self::$userName = $userName;
+        self::$userPassword = $userPassword;
     }
+
     /**
      * 获取mq连接
      * @return AMQPStreamConnection
      */
     public static function getMqConnection()
     {
-        self::$host=env("MQ_HOST");
-        self::$port=env("MQ_PORT");
-        self::$userName=env("MQ_USER_NAME");
-        self::$userPassword=env("MQ_USER_PASSWORD");
+        self::$host = env("MQ_HOST");
+        self::$port = env("MQ_PORT");
+        self::$userName = env("MQ_USER_NAME");
+        self::$userPassword = env("MQ_USER_PASSWORD");
         //连接mq
-        $connection = new AMQPStreamConnection(self::$host,self::$port,self::$userName,self::$userPassword);
+        $connection = new AMQPStreamConnection(self::$host, self::$port, self::$userName, self::$userPassword);
         return $connection;
     }
 
@@ -67,7 +69,7 @@ class MqService
      * @param string $message 消息
      * @param string $routeKey 路由键
      */
-    public static function pushLogMessage($message,$routeKey)
+    public static function pushLogMessage($message, $routeKey)
     {
         $connection = self::getMqConnection();
         //创建channel
@@ -81,7 +83,7 @@ class MqService
         //创建mq消息
         $msg = new AMQPMessage($message);
         //推送消息到mq
-        $channel->basic_publish($msg,"logExchange",$routeKey);
+        $channel->basic_publish($msg, "logExchange", $routeKey);
 
         //发送成功之后关闭channel和connection
         $channel->close();
@@ -94,7 +96,7 @@ class MqService
      * @param string $exchange 交换机
      * @param string $routeKey 路由键
      */
-    public static function pushMessage($message,$exchange,$routeKey)
+    public static function pushMessage($message, $exchange, $routeKey)
     {
         $connection = self::getMqConnection();
         //创建channel
@@ -103,7 +105,7 @@ class MqService
         //创建mq消息
         $msg = new AMQPMessage($message);
         //推送消息到mq
-        $channel->basic_publish($msg,$exchange,$routeKey);
+        $channel->basic_publish($msg, $exchange, $routeKey);
 
         //发送成功之后关闭channel和connection
         $channel->close();