我正在尝试使用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>
Run Code Online (Sandbox Code Playgroud)
这是我发布的服务器(这里我试图将这些数据发布到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 …
Run Code Online (Sandbox Code Playgroud)