我想知道如何从php发送multipart / form-data编码的post请求 . 我已在此处找到此代码:
$destination = "http://yourdomain.com/yoururl";
$eol = "\r\n";
$data = '';
$mime_boundary=md5(time());
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="somedata"' . $eol . $eol;
$data .= "Some Data" . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="somefile";
filename="filename.ext"' . $eol;
$data .= 'Content-Type: text/plain' . $eol;
$data .= 'Content-Transfer-Encoding: base64' . $eol . $eol;
$data .= chunk_split(base64_encode("Some file content")) . $eol;
$data .= "--" . $mime_boundary . "--" . $eol . $eol; // finish with two
eol's!!
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/form-data; boundary='
. $mime_boundary . $eol,
'content' => $data
));
$ctx = stream_context_create($params);
$response = @file_get_contents($destination, FILE_TEXT, $ctx);
(http://vedovini.net/2009/08/posting-multipart-form-data-using-php/)我的问题是:如何定义应该发送的文件? (只是为了解释我喜欢做什么:用户上传一个带有简单http表格的文件,php脚本接收它并且应该"resend"该文件到另一个URL) . 我还必须在json数组中发送1个变量以及该文件 .
1 回答
使用cURL而不是file_get_contents,关于将其与多部分POST提交here一起使用有类似的问题 .