cURL image upload using multipart/form-data -
i have been 1 month dealing following code post image using curl remote server.
function post_file($url) { $file_url = "6.jpg"; $eol = "\r\n"; $boundary = '111151173914518'; $body=""; //init curl body $body.= '-----------------------------'.$boundary. $eol; $body .= 'content-disposition: form-data; name="input1"' . $eol . $eol; $body .= "left" . $eol; $body.= '-----------------------------'.$boundary. $eol; $body.= 'content-disposition: form-data; name="input2"' . $eol . $eol ; $body .= "right" . $eol; $body.= '-----------------------------'.$boundary. $eol; $body.= 'content-disposition: form-data; name="input3"' . $eol . $eol ; $body .= "up" . $eol; $body.= '-----------------------------'.$boundary. $eol; $body.= 'content-disposition: form-data; name="input4"' . $eol . $eol ; $body .= "down" . $eol; $body.= '-----------------------------'.$boundary. $eol; $body.= 'content-disposition: form-data; name="input5"' . $eol . $eol ; $body .= "center" . $eol; $body.= '-----------------------------'.$boundary. $eol; $body.= 'content-disposition: form-data; name="input6"' . $eol . $eol ; $body .= "middle" . $eol; $body.= '-----------------------------'.$boundary. $eol; $body.= 'content-disposition: form-data; name="files[]"; filename="6.jpg"' . $eol; $body.= 'content-type: application/octet-stream'. $eol; / $body.= 'content-transfer-encoding: multipart/form-data' . $eol . $eol; $body.= file_get_contents($file_url) . $eol; $body.= '-----------------------------'.$boundary .'--' . $eol. $eol; $post_file = curl_init(); //init curl curl_setopt($post_file, curlopt_httpheader, array("content-type: multipart/form-data;boundary=---------------------------".$boundary) ); curl_setopt($post_file, curlopt_cookiejar, "cookie.txt"); curl_setopt($post_file, curlopt_cookiefile, "cookie.txt"); curl_setopt($post_file, curlopt_timeout, 40000); curl_setopt($post_file, curlopt_returntransfer, true); curl_setopt($post_file, curlopt_url, $url); curl_setopt($post_file, curlopt_useragent, $_server['http_user_agent']); curl_setopt($post_file, curlopt_followlocation, true); curl_setopt($post_file, curlopt_post, true); curl_setopt($post_file, curlopt_postfields, $body);}
it has been no issue input1 input6, there no image has been uploaded.
but when upload using curl, following result:
request headers: post /server.php http/1.1 host: myserver.com accept: */* content-type: multipart/form-data;boundary=-------------------------- -111151173914518 content-length: 42987 expect: 100-continue response: http/1.1 100 continue http/1.1 200 ok content-type: text/html; charset=utf-8 server: microsoft-iis/8.5 x-powered-by: php/5.6.10 x-powered-by: asp.net date: mon, 31 aug 2015 07:12:02 gmt content-length: 171 array ( 'input1' => 'left', 'input2' => 'right', 'input3' => 'up', 'input4' => 'down', 'input5' => 'center', 'input6' => 'middle', )
all inputs there except image have tried change content type application/octet-stream image/jpeg , content transfer encoding multipart/form-data binary , base64 not working.
Comments
Post a Comment