php 对接midjourney
时间: 2023-11-21 07:49:41 浏览: 128
要在PHP中对接Midjourney API进行文本转图片,可以使用以下示例代码:
```php
$api_url = "https://api.midjourney.com/api/v1/image/generate";
$data = array(
"text" => "Hello, world!",
"width" => 600,
"height" => 400,
"font_size" => 30,
"font_family" => "serif",
"bg_color" => "#f8f8f8"
);
$options = array(
"http" => array(
"method" => "POST",
"header" => "Content-Type: application/json\r\n",
"content" => json_encode($data)
)
);
$context = stream_context_create($options);
$response = file_get_contents($api_url, false, $context);
file_put_contents("output.png", $response);
```
以上代码使用了PHP内置函数`file_get_contents()`发起POST请求,将API返回的图片保存到名为`output.png`的文件中。你可以根据自己的需求修改`$data`参数。
阅读全文