rkljw 6 months ago
parent
commit
ea79d6d4ac
2 changed files with 229 additions and 0 deletions
  1. 227 0
      app/Controller/SseController.php
  2. 2 0
      config/api/collector.php

+ 227 - 0
app/Controller/SseController.php

@@ -63,4 +63,231 @@ class SseController extends AbstractController
         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;
+    }
+
 }

+ 2 - 0
config/api/collector.php

@@ -9,6 +9,8 @@ use App\Controller\CollectorController;
 //智普测试接口
 Router::addGroup('/chat', function () {
     Router::get('/stream', 'App\Controller\SseController@stream');
+    Router::get('/nyn', 'App\Controller\SseController@nyn');
+
 });
 Router::addGroup(
     '/collector', function () {