我正在尝试使用multipart / form-data标头在PHP中使用cURL发布图像,因为我发送给的API期望图像以多部分形式发送。
使用其他请求与API通讯时,我没有任何问题;仅发布图像是一个问题。
我在客户端使用此表单:
<form action="http://myServerURL" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="Submit">
</form>
这是我要发布到的服务器(在这里我试图将此数据转发到API):
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $imgRawData); // <-- raw data here hm?
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieJar);
curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); <-- using this as I wanted to check if HTTPHEADER is set
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data')); <-- setting content-type header?
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
// i get response from the server
$response = curl_exec( $ch );
// with this I can check what kind of content type the last request had?
$requestContentType = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
echo "<br>request Content Type was:".$requestContentType."<br>";
curl_close($ch);
echo "<br><b>SERVER POST IMAGE RESPONSE:</b><br>";
echo $response;
使用下面的代码,我可以看到我的请求标头:
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
var_dump(curl_getinfo($ch));
现在可以正确显示请求标题中的内容类型。但是似乎图像未按照API的预期正确发送。不幸的是,我无权访问API ...
任何帮助表示赞赏,谢谢
精慕HU
宝慕林4294392