本地使用秋叶整合包搭建了一个SD模型,对应接口是 http://127.0.0.1:7860/sdapi/v1/txt2img
因为调用是json,返回也是json
文档是:
在POST方法中请求返回
我看到方法似乎是用Stream实现的,这块没有太了解过,看不太懂
protected function post($url, $data, $options, $headers = [])
{
// 如果url以/remove-background结尾
$removeButtons = [];
if (strpos($url, '/remove-background') !== false) {
$removeButtons = ['remove-background'];
}
$headers = array_merge([
// 'Authorization' => "Bearer " . $this->apikey ?: '',
// 'Accept' => 'image/*'
], $headers);
$options = $this->formatOptions($options);
$multipart = [];
foreach ($data as $field => $value) {
if (is_resource($value)) {
$multipart[] = [
'name' => $field,
'contents' => $value
];
} else {
$multipart[] = [
'name' => $field,
'contents' => $value,
];
}
}
$multipart = new \Workerman\Psr7\MultipartStream($multipart);
$boundary = $multipart->getBoundary();
$headers['Content-Type'] = "application/json";
$requestOptions = [
'method' => 'POST',
'data' => json_encode($data),
'headers' => $headers,
'success' => function (Response $response) use ($options, $removeButtons) {
$options['complete'](static::formatResponse((string)$response->getBody(), $removeButtons), $response);
},
'error' => function ($exception) use ($options) {
// var_dump($exception);
// var_dump($options);
$options['complete']([
'error' => [
'code' => 'exception',
'message' => $exception->getMessage(),
'detail' => (string) $exception
],
], new Response(0));
}
];
$http = new Client(['timeout' => 600]);
$http->request($url, $requestOptions);
}
我需要怎么调整才能保证和原有接口返回一致呢?
牛人
最新优化方案:
plugin\sd\app\controller\ImageController 不做任何改变
修改:
plugin\sd\app\handler\driver\Sd
create 方法
post 方法:
formatResponse 方法
改为: