【发布时间】:2014-02-20 11:10:24
【问题描述】:
我正在尝试使用 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...
感谢您的帮助,谢谢
【问题讨论】:
-
在对我的结构等进行了一些小的修改后,这有效。如果需要,您可以发布作为答案...
-
没关系,如果它对你有用:)
标签: php curl multipartform-data