猿问

在curl中将原始图像数据作为多部分/表单数据发布

我正在尝试使用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 ...


任何帮助表示赞赏,谢谢


慕婉清6462132
浏览 533回答 2
2回答

精慕HU

从PHP 5.6开始@$filePath,CURLOPT_POSTFIELDS如果不CURLOPT_SAFE_UPLOAD进行设置,它将无法使用,并且在PHP 7中将其完全删除。您将需要使用CurlFile对象RFC此处。$fields = [&nbsp; &nbsp; 'name' => new \CurlFile($filePath, 'image/png', 'filename.png')];curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);

宝慕林4294392

如果任何人有同样的问题:检查这是@PravinS建议。我使用的代码与此处显示的完全相同,并且非常适合我。这是服务器代码的相关部分,它有助于:if (isset($_POST['btnUpload'])){$url = "URL_PATH of upload.php"; // e.g. http://localhost/myuploader/upload.php // request URL$filename = $_FILES['file']['name'];$filedata = $_FILES['file']['tmp_name'];$filesize = $_FILES['file']['size'];if ($filedata != ''){&nbsp; &nbsp; $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading&nbsp; &nbsp; $postfields = array("filedata" => "@$filedata", "filename" => $filename);&nbsp; &nbsp; $ch = curl_init();&nbsp; &nbsp; $options = array(&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_URL => $url,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_HEADER => true,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_POST => 1,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_HTTPHEADER => $headers,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_POSTFIELDS => $postfields,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_INFILESIZE => $filesize,&nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_RETURNTRANSFER => true&nbsp; &nbsp; ); // cURL options&nbsp; &nbsp; curl_setopt_array($ch, $options);&nbsp; &nbsp; curl_exec($ch);&nbsp; &nbsp; if(!curl_errno($ch))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $info = curl_getinfo($ch);&nbsp; &nbsp; &nbsp; &nbsp; if ($info['http_code'] == 200)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $errmsg = "File uploaded successfully";&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $errmsg = curl_error($ch);&nbsp; &nbsp; }&nbsp; &nbsp; curl_close($ch);}else{&nbsp; &nbsp; $errmsg = "Please select the file";}}html表单应类似于:<form action="uploadpost.php" method="post" name="frmUpload" enctype="multipart/form-data"><tr>&nbsp; <td>Upload</td>&nbsp; <td align="center">:</td>&nbsp; <td><input name="file" type="file" id="file"/></td></tr><tr>&nbsp; <td>&nbsp;</td>&nbsp; <td align="center">&nbsp;</td>&nbsp; <td><input name="btnUpload" type="submit" value="Upload" /></td></tr>
随时随地看视频慕课网APP
我要回答