SseController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Hyperf\Di\Annotation\Inject;
  5. use Hyperf\HttpServer\Contract\RequestInterface;
  6. use Hyperf\HttpServer\Contract\ResponseInterface;
  7. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  8. use Psr\Http\Message\ServerRequestInterface;
  9. use App\Tools\Result;
  10. class SseController extends AbstractController
  11. {
  12. #[Inject]
  13. protected ValidatorFactoryInterface $validationFactory;
  14. public function stream(RequestInterface $request, ResponseInterface $response)
  15. {
  16. $requireData = $this->request->all();
  17. var_dump("=======",$request->all(),"+++++++++",$requireData);
  18. $validator = $this->validationFactory->make(
  19. $requireData,
  20. [
  21. 'problem'=> 'required',
  22. ],
  23. [
  24. 'problem.required' => '请提出你的问题',
  25. ]
  26. );
  27. if ($validator->fails()) {
  28. $errorMessage = $validator->errors()->first();
  29. return Result::error($errorMessage);
  30. }
  31. $params = [
  32. "model"=>"glm-4v-flash",
  33. "messages"=>[
  34. [
  35. "role"=>"system",
  36. "content"=>"你是一个乐于解答各种问题的助手,你的任务是为用户提供专业、准确、有见地的建议。"//$requireData['message']
  37. ],
  38. [
  39. "role"=>"user",
  40. "content"=>$requireData['problem']//$requireData['message']
  41. ]
  42. ],
  43. "stream"=>true
  44. ];
  45. $apiUrl = "https://open.bigmodel.cn/api/paas/v4/chat/completions";
  46. //93a716c97e7848329914efe5d036c896.0feTS9QVahSzXMD8
  47. $result= $this->callApi($apiUrl, 'POST', $params, ['Authorization: Bearer 93a716c97e7848329914efe5d036c896.0feTS9QVahSzXMD8']);
  48. $arr = preg_split('/\n\s*\n/', $result);
  49. // 开始流式传输
  50. $this->container->get(\Swoole\Http\Response::class)->status(200);
  51. foreach ($arr as $val){
  52. $response->write("$val\n\n");
  53. usleep(1000000); // 暂停1秒
  54. }
  55. return $response;
  56. }
  57. function callApi($url, $method = 'GET', $data = null, $headers = [])
  58. {
  59. $ch = curl_init();
  60. // 配置 CURL
  61. curl_setopt($ch, CURLOPT_URL, $url);
  62. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  63. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  64. // 如果是 POST/PUT 请求,附加数据
  65. if ($data && ($method === 'POST' || $method === 'PUT')) {
  66. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  67. $headers[] = 'Content-Type: application/json';
  68. }
  69. // 附加头部信息
  70. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  71. // 执行请求
  72. $response = curl_exec($ch);
  73. // 错误处理
  74. if (curl_errno($ch)) {
  75. $response = json_encode(['error' => curl_error($ch)]);
  76. }
  77. curl_close($ch);
  78. return $response;
  79. }
  80. /**
  81. * 通一千问
  82. * @return void
  83. */
  84. public function nyn()
  85. {
  86. //先检测图片是否合规
  87. // $this->aaDetect();
  88. // $this->aaTemplateGeneration();
  89. $this->getTasks();
  90. // $this->createVideoSynthesis();
  91. }
  92. //http://192.168.1.201:9501/image/20250110/1736476587912702.mp4
  93. public function aaDetect()
  94. {
  95. // 设置您的API密钥和图像URL
  96. $apiKey = 'sk-1816fc5849dc4f2b8752f624e087da04'; // 替换为您的实际API密钥
  97. $imageUrl = 'http://103.105.201.2:19000/dev/1233214.jpg'; // 替换为实际的图像URL
  98. // 初始化cURL会话
  99. $ch = curl_init();
  100. // 设置cURL选项
  101. curl_setopt($ch, CURLOPT_URL, "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/aa-detect");
  102. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  103. curl_setopt($ch, CURLOPT_POST, 1);
  104. // 设置请求头
  105. $headers = array(
  106. 'Authorization: Bearer ' . $apiKey,
  107. 'Content-Type: application/json'
  108. );
  109. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  110. // 设置请求体数据
  111. $data = array(
  112. "model" => "animate-anyone-detect-gen2",
  113. "input" => array(
  114. "image_url" => $imageUrl
  115. ),
  116. "parameters" => new \stdClass() // 确保即使parameters为空,它也是有效的JSON对象
  117. );
  118. // 将数组转换为JSON格式
  119. $jsonData = json_encode($data);
  120. // 检查是否成功转换为JSON
  121. if ($jsonData === false) {
  122. echo 'JSON encoding failed';
  123. } else {
  124. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
  125. // 执行cURL会话并获取响应
  126. $response = curl_exec($ch);
  127. if (curl_errno($ch)) {
  128. echo 'Error:' . curl_error($ch);
  129. } else {
  130. // 输出响应结果
  131. echo $response;
  132. }
  133. }
  134. // 关闭cURL资源,并释放系统资源
  135. curl_close($ch);
  136. // $params = [
  137. // "model"=>"animate-anyone-template-gen2",
  138. // "input"=>[
  139. // "image_url"=>"http://183.131.25.186:9501/image/20250110/1736470414103503.png"
  140. // ],
  141. // "parameters"=>[
  142. // "size"=>"720*1280"
  143. // ]
  144. // ];
  145. // $apiUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/aa-detect";
  146. // $result= $this->qianwenApi($apiUrl, 'POST', $params, ['Authorization: Bearer sk-1816fc5849dc4f2b8752f624e087da04','Content-Type:application/json']);
  147. // var_dump("图片检测:",$result);
  148. }
  149. public function aaTemplateGeneration()
  150. {
  151. // 设置您的API密钥和图像URL
  152. $apiKey = 'sk-1816fc5849dc4f2b8752f624e087da04'; // 替换为您的实际API密钥
  153. $imageUrl = 'http://103.105.201.2:19000/dev/123.mp4'; // 替换为实际的图像URL
  154. // 初始化cURL会话
  155. $ch = curl_init();
  156. // 设置cURL选项
  157. curl_setopt($ch, CURLOPT_URL, "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/aa-template-generation");
  158. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  159. curl_setopt($ch, CURLOPT_POST, 1);
  160. // 设置请求头
  161. $headers = array(
  162. 'Authorization: Bearer ' . $apiKey,
  163. 'Content-Type: application/json',
  164. 'X-DashScope-Async:enable'
  165. );
  166. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  167. // 设置请求体数据
  168. $data = array(
  169. "model" => "animate-anyone-template-gen2",
  170. "input" => array(
  171. "video_url" => $imageUrl
  172. ),
  173. "parameters" => new \stdClass() // 确保即使parameters为空,它也是有效的JSON对象
  174. );
  175. // 将数组转换为JSON格式
  176. $jsonData = json_encode($data);
  177. // 检查是否成功转换为JSON
  178. if ($jsonData === false) {
  179. echo 'JSON encoding failed';
  180. } else {
  181. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
  182. // 执行cURL会话并获取响应
  183. $response = curl_exec($ch);
  184. if (curl_errno($ch)) {
  185. echo 'Error:' . curl_error($ch);
  186. } else {
  187. // 输出响应结果
  188. echo $response;
  189. }
  190. }
  191. // 关闭cURL资源,并释放系统资源
  192. curl_close($ch);
  193. }
  194. public function getTasks()
  195. {
  196. $a = $this->qianwenApi("https://dashscope.aliyuncs.com/api/v1/tasks/84870a2f-9c7c-4130-b2fd-07e8d2d389e1","GET","", ['Authorization: Bearer sk-1816fc5849dc4f2b8752f624e087da04']);
  197. var_dump("测试数据:",$a);
  198. }
  199. /**
  200. * 创建视频
  201. * @return void
  202. */
  203. public function createVideoSynthesis()
  204. {
  205. // 设置您的API密钥和图像URL
  206. $apiKey = 'sk-1816fc5849dc4f2b8752f624e087da04'; // 替换为您的实际API密钥
  207. $imageUrl = 'http://103.105.201.2:19000/dev/1233214.jpg'; // 替换为实际的图像URL
  208. // 初始化cURL会话
  209. $ch = curl_init();
  210. // 设置cURL选项
  211. curl_setopt($ch, CURLOPT_URL, "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/video-synthesis");
  212. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  213. curl_setopt($ch, CURLOPT_POST, 1);
  214. // 设置请求头
  215. $headers = array(
  216. 'Authorization: Bearer ' . $apiKey,
  217. 'Content-Type: application/json',
  218. 'X-DashScope-Async:enable'
  219. );
  220. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  221. // 设置请求体数据
  222. $data = array(
  223. "model" => "animate-anyone-gen2",
  224. "input" => array(
  225. "image_url" => $imageUrl,
  226. "template_id"=>'AACT.8090e67b.ldsiTs8hEe-V9opVewhVuA.hImdayoq'
  227. ),
  228. "parameters" => array(
  229. "use_ref_img_bg"=>false,
  230. "video_ratio"=>"9:16"
  231. ) // 确保即使parameters为空,它也是有效的JSON对象
  232. );
  233. // 将数组转换为JSON格式
  234. $jsonData = json_encode($data);
  235. // 检查是否成功转换为JSON
  236. if ($jsonData === false) {
  237. echo 'JSON encoding failed';
  238. } else {
  239. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
  240. // 执行cURL会话并获取响应
  241. $response = curl_exec($ch);
  242. if (curl_errno($ch)) {
  243. echo 'Error:' . curl_error($ch);
  244. } else {
  245. // 输出响应结果
  246. echo $response;
  247. }
  248. }
  249. }
  250. function qianwenApi($url, $method = 'GET', $data = null, $headers = [])
  251. {
  252. $ch = curl_init();
  253. // 配置 CURL
  254. curl_setopt($ch, CURLOPT_URL, $url);
  255. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  256. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  257. // 如果是 POST/PUT 请求,附加数据
  258. if ($data && ($method === 'POST' || $method === 'PUT')) {
  259. var_dump("转数据:",json_encode($data));
  260. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  261. }
  262. // 附加头部信息
  263. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  264. // 执行请求
  265. $response = curl_exec($ch);
  266. // 错误处理
  267. if (curl_errno($ch)) {
  268. $response = json_encode(['error' => curl_error($ch)]);
  269. }
  270. curl_close($ch);
  271. return $response;
  272. }
  273. }