[英]Php Curl send File with data
我需要上传3个文件以及发布数据中的变量。 这就是我的电话-
$data['type1'] = new CurlFile($file1);
$data['type2'] = new CurlFile($file2);
$data['type3'] = new CurlFile($file3);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data, "var1: $val1", "var2: $val2");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data', "headkey: $headkeyValue"));
我无法获得$app->request()->post('var1');
从苗条的框架。 它是空的。
$app->request()->headers('headkey');
headkey
$app->request()->headers('headkey');
这是由于所有使用HTTP POST方法上传的文件都位于$_FILES
全局变量中。 这就是为什么您不能通过这种方式访问文件
$app->request()->post('val1');
但您可以使用$_FILES
$_FILES
说明
通过HTTP POST方法上传到当前脚本的项目的关联数组。 POST方法上载部分概述了此数组的结构。
这里的样品卷曲要求
$curlFile = curl_file_create($uploaded_file_name_with_full_path);
$post = array('val1' => 'value','val2' => 'value','file_contents'=> $curlFile );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$your_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
不要忘记放置适当的标题。
您还可以在此处找到良好的来源Curl File Upload,以通过CURL发送文件。 确保在上述代码中的file_contents
键上传递文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.