dtcpvz8162 2010-07-09 00:33
浏览 786
已采纳

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

How do I specify the content type of a specific part of a multipart/form-data request? The content type for the image is being sent as application/octet-stream, however the server is expecting it to be image/jpeg. This causes the server to deny my request.

$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);

This is the relevant parts of the request:

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

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

I want it to be:

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

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

1条回答 默认 最新

  • dpowt82802 2010-07-09 01:58
    关注

    You would do something like this,

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题