multipart/form-data is a standard upload protocol, regardless of the language of the server.
the curl command line uses the parameter-- form to specify the upload file. Check the curl option of php, and there should be something corresponding to it. There is no need to manually set the Content-Type and Content-Length headers.
I have given me some advice before, and I still think you should take more basic courses, but as an interviewer, you will find it easier to blame newcomers.
according to the description of your problem, curl is used to upload data to the other party. My way of thinking is
1, and some people have mentioned above. Then you will get a string of base64 encoded characters
2, and transmit these base64 encoded characters using curl's post method, which is equivalent to your server's curl simulation form post submission to each other
3, and the receiver is nodejs,. Then you need the other party to write an API interface that accepts post data, and the other party's API needs to include the function of base64 decoding, and convert the encoding you sent to save the picture again
.
first step, assuming that 1.jpg is the picture you need to upload, start converting the picture
function base64_encode_image ($file_location=string,$filetype=string) {
if ($file_location) {
$imgbinary = fread(fopen($file_location, "r"), filesize($file_location));
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}
}
//$file_location$filetypejpgpngbmp
$encoded = base64_encode_image ($fread);
the second step is to transmit these base64 encoded characters through the post method of curl,
//$encodedbase64
/**
* posturl
* @param string $url
* @param string $param
*/
function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init();//curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//
curl_setopt($ch, CURLOPT_HEADER, 0);//header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//
curl_setopt($ch, CURLOPT_POST, 1);//post
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//curl
curl_close($ch);
return $data;
}
//$encodedbase64$urlnodejsAPIURL
request_post($url, $encoded);
the third step is that nodejs, is not the focus of this question, and I don't know too much nodejs, and don't answer much
the above use cases are quoted from
http://php.net/manual/zh/func.
https://www.cnblogs.com/ps-bl.
list the base64 bar
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => new CURLFille('/home/vagrant/test.png'));
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/load_file.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
$aStatus = curl_getinfo($ch);