Use PHP curl to send POST with Content-Type: image/jpeg multipart/form-data;

870 Views Asked by At

A post request is made to:

https://market.kz/upload/?

and the post data is as follows:

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

I use next code:

$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;

but my code is incorrect, how should create array $fields?

0

There are 0 best solutions below