提问人:trogwar 提问时间:11/8/2023 最后编辑:trogwar 更新时间:11/8/2023 访问量:33
在 Guzzle 中模拟 curl 工作脚本(多部分数据与二进制一起上传)
Mimic curl working script in Guzzle (multipart data with binary uploads together)
问:
我有完美运行的 curl 命令(通过 Stability API 使用 img2img):
curl --request POST 'https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/image-to-image' \
--header 'Content-Type: multipart/form-data' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer somesecrettoken' \
--form 'init_image=@"/full/path/to/init_image.png"' \
--form 'text_prompts[0][text]=Beautiful php code' \
--output '/full/path/to/response.json'
我已经用 Guzzle v.6.5.8 尝试了这个 PHP 代码,它给了我 HTTP 520:
(new Client())
->post(
'https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/image-to-image',
[
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer somesecrettoken',
'Content-Type' => 'multipart/form-data',
],
RequestOptions::MULTIPART => [
[
'name' => 'text_prompts[0][text]',
'contents' => 'Beautiful php code',
],
[
'name' => 'init_image',
'contents' => (new File('/full/path/to/init_image.png'))->getContent(),
// 'contents' => \GuzzleHttp\Psr7\Utils::tryFopen('/full/path/to/init_image.png', 'rb'), // This won't work too
],
],
RequestOptions::SINK => '/full/path/to/response.json',
RequestOptions::DEBUG => true,
],
)
我在这里做错了什么?
答:
1赞
CBroe
11/8/2023
#1
假设它只包含 你的 ,你的标题将缺少关键字。app.stability.key
somesecrettoken
Authorization
Bearer
此外,省略标题可能是一个好主意。此标头最终需要包含库用于分隔此请求的各个部分的边界值 - 并且在使用 时,它应该自行添加此标头,包括边界值。如果显式指定它,则可能会覆盖库本身创建的内容,然后边界将丢失。Content-Type
RequestOptions::MULTIPART
评论
app.stability.key
somesecrettoken
Authorization
Bearer
Content-Type
RequestOptions::MULTIPART
text-to-image