php调用阿里云的flux文生图模型api生成图片示例代码

代码语言:php

所属分类:其他

代码描述:php调用阿里云的flux文生图模型api生成图片示例代码,异步队列,先生成一个文生图任务,然后根据任务id查询人物执行结果。

代码标签: php 调用 阿里云 flux 文生图 模型 api 生成 图片 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php


function submitTask($apiKey, $prompt, $size = "768*512", $seed = 42, $steps = 4) {
    $url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis";
    $headers = [
        "Content-Type: application/json",
        "Authorization: Bearer $apiKey",
        "X-DashScope-Async: enable"
    ];

    $data = [
        "model" => "flux-schnell",
        "input" => [
            "prompt" => $prompt
        ],
        "parameters" => [
            "size" => $size,
            "seed" => $seed,
            "steps" => $steps
        ]
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}
function queryTaskStatus($apiKey, $taskId) {
    $url = "https://dashscope.aliyuncs.com/api/v1/tasks/$taskId";
    $headers = [
        "Authorization: Bearer $apiKey"
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPGET, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}


// Example usage

//key申请地址:https://dashscope.console.aliyun.com/apiKey

$apiKey = "apikey&quo.........完整代码请登录后点击上方下载按钮下载查看

网友评论0