使用PHP curl发送带有Content-Type的POST:image / jpeg multipart / form-data;

时间:2016-12-19 06:24:34

标签: php curl multipartform-data content-type form-data

发布请求:

https://market.kz/upload/?

,帖子数据如下:

Content-Disposition: form-data; name="filedata"; filename="20161206_131508.jpg.jpeg"
Content-Type: image/jpeg

我使用下一个代码:

$urlTo2 = 'https://market.kz/upload/?';
$fields = array(
    'filedata' => $base64image,
    'filename' => '20161206_131508.jpg.jpeg'
);

$fields_string = '';

foreach($fields as $key=>$value) {
    $fields_string .= $key.'='.$value.'&'; 
}
rtrim($fields_string, '&');
$post2 = '';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlTo2);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36');
curl_setopt($ch, CURLOPT_REFERER, $urlTo1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

但是我的代码不正确,应该如何创建数组$ fields?

0 个答案:

没有答案