rkljw hace 4 meses
padre
commit
b05ab544c3
Se han modificado 3 ficheros con 121 adiciones y 1 borrados
  1. 56 0
      app/Controller/CollectorController.php
  2. 62 0
      app/Tools/PublicData.php
  3. 3 1
      config/api/collector.php

+ 56 - 0
app/Controller/CollectorController.php

@@ -148,6 +148,62 @@ class CollectorController extends AbstractController
 //        $this->collectorServiceClient->sendCrawler($data);
 //        return Result::success([]);
 //    }
+      public function zhipu()
+      {
+          $requireData = $this->request->all();
+          // 接口URL
+          $apiUrl = 'https://open.bigmodel.cn/api/paas/v4/chat/completions';
+            // API密钥
+          $apiKey = 'be1856920c54ac537b530d69bc2eda73.gOO2BMq9NXavzEMq';
+          // 请求参数
+          $params = [
+              "model"=>"glm-4",
+              "messages"=>[
+                  [
+                      "role"=>"user",
+                      "content"=>$requireData['message']
+                  ]
+
+              ]
+          ];
+          // 构建请求头部
+          $headers = [
+              'Content-Type: application/json',
+              'Authorization: Bearer ' . $apiKey
+          ];
+
+        // 将参数转换为JSON格式
+          $jsonParams = json_encode($params);
+          var_dump("看看:",$jsonParams);
+        // 初始化cURL会话
+          $ch = curl_init($apiUrl);
+          // 设置cURL选项
+          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+          curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
+          curl_setopt($ch, CURLOPT_POST, true);
+          curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonParams);
+
+        // 执行cURL会话
+          $response = curl_exec($ch);
+
+        // 检查是否有错误发生
+          if (curl_errno($ch)) {
+              echo 'cURL error: ' . curl_error($ch);
+          } else {
+              // 处理响应
+              $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+              if ($httpCode == 200) {
+                  // 解码JSON响应
+                  $responseData = json_decode($response, true);
+                  Result::success($responseData);
+              } else {
+                  echo "HTTP error: $httpCode\n";
+                  echo "Response: $response\n";
+              }
+          }
+// 关闭cURL会话
+          curl_close($ch);
+      }
 
 
 }

+ 62 - 0
app/Tools/PublicData.php

@@ -127,4 +127,66 @@ class PublicData
         }
     }
 
+    /**
+     * POST 请求
+     * @param string $url
+     * @param array $param
+     * @param boolean $post_file 是否文件上传
+     * @return string content
+     */
+    public static function http_post_zp($url, $data, $options = [])
+    {
+
+        // 初始化CURL会话
+        $ch = curl_init($url);
+        var_dump("参数:",$data);
+        // 设置CURL选项
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
+        curl_setopt($ch, CURLOPT_HEADER, false); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
+        curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的POST请求。
+        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // 要传递的POST数据,这里使用http_build_query将数组转换为URL编码的查询字符串。
+        $headers = [
+            'Authorization: Bearer be1856920c54ac537b530d69bc2eda73.gOO2BMq9NXavzEMq',
+            'Content-Type: application/json',
+//            'Custom-Header: customHeaderValue'
+        ];
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+        // 如果有额外的CURL选项,则合并它们
+        if (!empty($options)) {
+            curl_setopt_array($ch, $options);
+        }
+
+        // 执行CURL会话并获取响应
+        $response = curl_exec($ch);
+        // 检查是否有CURL错误
+        if (curl_errno($ch)) {
+            $error_msg = curl_error($ch);
+            curl_close($ch);
+            throw new Exception("CURL Error: $error_msg");
+        }
+
+        // 获取HTTP状态码
+        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+
+        // 关闭CURL会话
+        curl_close($ch);
+
+        // 返回一个包含响应和HTTP状态码的数组
+        $responseBody = $response;
+        $headerEnd = strpos($response, "\r\n\r\n");
+
+        if ($headerEnd !== false) {
+            // 去除响应头,只保留响应体
+            $responseBody = substr($response, $headerEnd + 4); // +4 是因为 "\r\n\r\n" 有4个字符
+//            echo $responseBody; // 输出:This is the response body.
+        } else {
+            // 如果没有找到空行,可能响应格式不正确或没有响应头
+//            echo "No headers found in response.";
+        }
+        return [
+            'response' => $responseBody,
+            'http_code' => $http_code
+        ];
+    }
+
 }

+ 3 - 1
config/api/collector.php

@@ -41,7 +41,9 @@ Router::addGroup(
         Router::get('/addCatid', [CollectorController::class, 'addCatid']);
         
         Router::post('/sendCrawler', [CollectorController::class, 'sendCrawler']);
-        
+        //智普测试接口
+        Router::post('/zhipu', [CollectorController::class, 'zhipu']);
+
     },
     ['middleware' => [FooMiddleware::class]]
 );