gpt4 book ai didi

php - 在PHP的curl中指定multipart/form-data各部分的Content-Type

转载 作者:可可西里 更新时间:2023-10-31 22:59:42 25 4
gpt4 key购买 nike

如何指定多部分/表单数据请求的特定部分的内容类型?图像的内容类型作为 application/octet-stream 发送,但服务器期望它是 image/jpeg。这会导致服务器拒绝我的请求。

$data["file"] = "@/image.jpg";
$data["title"] = "The title";
$data["description"] = "The description";

//make the POST request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);

这是请求的相关部分:

Content-Type: multipart/form-data; boundary=----------------------------fc57f743c490

------------------------------fc57f743c490
Content-Disposition: form-data; name="file"; filename="NASA-23.jpg"
Content-Type: application/octet-stream

我希望它是:

Content-Type: multipart/form-data; boundary=----------------------------fc57f743c490

------------------------------fc57f743c490
Content-Disposition: form-data; name="file"; filename="NASA-23.jpg"
Content-Type: image/jpeg

最佳答案

你会做这样的事情,

$data["file"] = "@/image.jpg;type=image/jpeg";

//make the POST request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);

关于php - 在PHP的curl中指定multipart/form-data各部分的Content-Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3209057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com