request->all(); var_dump("=======",$request->all(),"+++++++++",$requireData); $validator = $this->validationFactory->make( $requireData, [ 'problem'=> 'required', ], [ 'problem.required' => '请提出你的问题', ] ); if ($validator->fails()) { $errorMessage = $validator->errors()->first(); return Result::error($errorMessage); } $params = [ "model"=>"glm-4v-flash", "messages"=>[ [ "role"=>"system", "content"=>"你是一个乐于解答各种问题的助手,你的任务是为用户提供专业、准确、有见地的建议。"//$requireData['message'] ], [ "role"=>"user", "content"=>$requireData['problem']//$requireData['message'] ] ], "stream"=>true ]; $apiUrl = "https://open.bigmodel.cn/api/paas/v4/chat/completions"; //93a716c97e7848329914efe5d036c896.0feTS9QVahSzXMD8 $result= $this->callApi($apiUrl, 'POST', $params, ['Authorization: Bearer 93a716c97e7848329914efe5d036c896.0feTS9QVahSzXMD8']); $arr = preg_split('/\n\s*\n/', $result); // 开始流式传输 $this->container->get(\Swoole\Http\Response::class)->status(200); foreach ($arr as $val){ $response->write("$val\n\n"); usleep(1000000); // 暂停1秒 } return $response; } function callApi($url, $method = 'GET', $data = null, $headers = []) { $ch = curl_init(); // 配置 CURL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // 如果是 POST/PUT 请求,附加数据 if ($data && ($method === 'POST' || $method === 'PUT')) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $headers[] = 'Content-Type: application/json'; } // 附加头部信息 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 执行请求 $response = curl_exec($ch); // 错误处理 if (curl_errno($ch)) { $response = json_encode(['error' => curl_error($ch)]); } curl_close($ch); return $response; } /** * 通一千问 * @return void */ public function nyn() { //先检测图片是否合规 // $this->aaDetect(); // $this->aaTemplateGeneration(); $this->getTasks(); // $this->createVideoSynthesis(); } //http://192.168.1.201:9501/image/20250110/1736476587912702.mp4 public function aaDetect() { // 设置您的API密钥和图像URL $apiKey = 'sk-1816fc5849dc4f2b8752f624e087da04'; // 替换为您的实际API密钥 $imageUrl = 'http://103.105.201.2:19000/dev/1233214.jpg'; // 替换为实际的图像URL // 初始化cURL会话 $ch = curl_init(); // 设置cURL选项 curl_setopt($ch, CURLOPT_URL, "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/aa-detect"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); // 设置请求头 $headers = array( 'Authorization: Bearer ' . $apiKey, 'Content-Type: application/json' ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 设置请求体数据 $data = array( "model" => "animate-anyone-detect-gen2", "input" => array( "image_url" => $imageUrl ), "parameters" => new \stdClass() // 确保即使parameters为空,它也是有效的JSON对象 ); // 将数组转换为JSON格式 $jsonData = json_encode($data); // 检查是否成功转换为JSON if ($jsonData === false) { echo 'JSON encoding failed'; } else { curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); // 执行cURL会话并获取响应 $response = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } else { // 输出响应结果 echo $response; } } // 关闭cURL资源,并释放系统资源 curl_close($ch); // $params = [ // "model"=>"animate-anyone-template-gen2", // "input"=>[ // "image_url"=>"http://183.131.25.186:9501/image/20250110/1736470414103503.png" // ], // "parameters"=>[ // "size"=>"720*1280" // ] // ]; // $apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/aa-detect"; // $result= $this->qianwenApi($apiUrl, 'POST', $params, ['Authorization: Bearer sk-1816fc5849dc4f2b8752f624e087da04','Content-Type:application/json']); // var_dump("图片检测:",$result); } public function aaTemplateGeneration() { // 设置您的API密钥和图像URL $apiKey = 'sk-1816fc5849dc4f2b8752f624e087da04'; // 替换为您的实际API密钥 $imageUrl = 'http://103.105.201.2:19000/dev/123.mp4'; // 替换为实际的图像URL // 初始化cURL会话 $ch = curl_init(); // 设置cURL选项 curl_setopt($ch, CURLOPT_URL, "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/aa-template-generation"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); // 设置请求头 $headers = array( 'Authorization: Bearer ' . $apiKey, 'Content-Type: application/json', 'X-DashScope-Async:enable' ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 设置请求体数据 $data = array( "model" => "animate-anyone-template-gen2", "input" => array( "video_url" => $imageUrl ), "parameters" => new \stdClass() // 确保即使parameters为空,它也是有效的JSON对象 ); // 将数组转换为JSON格式 $jsonData = json_encode($data); // 检查是否成功转换为JSON if ($jsonData === false) { echo 'JSON encoding failed'; } else { curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); // 执行cURL会话并获取响应 $response = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } else { // 输出响应结果 echo $response; } } // 关闭cURL资源,并释放系统资源 curl_close($ch); } public function getTasks() { $a = $this->qianwenApi("https://dashscope.aliyuncs.com/api/v1/tasks/84870a2f-9c7c-4130-b2fd-07e8d2d389e1","GET","", ['Authorization: Bearer sk-1816fc5849dc4f2b8752f624e087da04']); var_dump("测试数据:",$a); } /** * 创建视频 * @return void */ public function createVideoSynthesis() { // 设置您的API密钥和图像URL $apiKey = 'sk-1816fc5849dc4f2b8752f624e087da04'; // 替换为您的实际API密钥 $imageUrl = 'http://103.105.201.2:19000/dev/1233214.jpg'; // 替换为实际的图像URL // 初始化cURL会话 $ch = curl_init(); // 设置cURL选项 curl_setopt($ch, CURLOPT_URL, "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); // 设置请求头 $headers = array( 'Authorization: Bearer ' . $apiKey, 'Content-Type: application/json', 'X-DashScope-Async:enable' ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 设置请求体数据 $data = array( "model" => "animate-anyone-gen2", "input" => array( "image_url" => $imageUrl, "template_id"=>'AACT.8090e67b.ldsiTs8hEe-V9opVewhVuA.hImdayoq' ), "parameters" => array( "use_ref_img_bg"=>false, "video_ratio"=>"9:16" ) // 确保即使parameters为空,它也是有效的JSON对象 ); // 将数组转换为JSON格式 $jsonData = json_encode($data); // 检查是否成功转换为JSON if ($jsonData === false) { echo 'JSON encoding failed'; } else { curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); // 执行cURL会话并获取响应 $response = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } else { // 输出响应结果 echo $response; } } } function qianwenApi($url, $method = 'GET', $data = null, $headers = []) { $ch = curl_init(); // 配置 CURL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // 如果是 POST/PUT 请求,附加数据 if ($data && ($method === 'POST' || $method === 'PUT')) { var_dump("转数据:",json_encode($data)); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } // 附加头部信息 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 执行请求 $response = curl_exec($ch); // 错误处理 if (curl_errno($ch)) { $response = json_encode(['error' => curl_error($ch)]); } curl_close($ch); return $response; } }