123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
- declare(strict_types=1);
- namespace App\Controller;
- use Hyperf\Di\Annotation\Inject;
- use Hyperf\HttpServer\Contract\RequestInterface;
- use Hyperf\HttpServer\Contract\ResponseInterface;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Psr\Http\Message\ServerRequestInterface;
- use App\Tools\Result;
- class SseController extends AbstractController
- {
- #[Inject]
- protected ValidatorFactoryInterface $validationFactory;
- public function stream(RequestInterface $request, ResponseInterface $response)
- {
- $requireData = $this->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;
- }
- }
|