简体   繁体   English

PHP的卷曲发送文件与数据

[英]Php Curl send File with data

I need to upload 3 files along with variables in post data. 我需要上传3个文件以及发布数据中的变量。 This is what my call looks like - 这就是我的电话-

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

I am not able to get $app->request()->post('var1'); 我无法获得$app->request()->post('var1'); from slim framework. 从苗条的框架。 It is empty. 它是空的。

  • I am able to get the headkey from the Header as $app->request()->headers('headkey'); 我可以从Header获取$app->request()->headers('headkey'); headkey $app->request()->headers('headkey');
  • I am able to get the data in $_FILES 我可以在$ _FILES中获取数据

It's due to the fact that all files uploaded with HTTP POST method are in $_FILES global variable. 这是由于所有使用HTTP POST方法上传的文件都位于$_FILES全局变量中。 That's why you cannot access files by this way 这就是为什么您不能通过这种方式访问​​文件

$app->request()->post('val1');

but you can by using $_FILES 但您可以使用$_FILES

$_FILES description $_FILES说明

An associative array of items uploaded to the current script via the HTTP POST method. 通过HTTP POST方法上传到当前脚本的项目的关联数组。 The structure of this array is outlined in the POST method uploads section. POST方法上载部分概述了此数组的结构。

Here the sample curl request 这里的样品卷曲要求

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

Don't forget to put the appropriate header. 不要忘记放置适当的标题。
You can also find good source here Curl File Upload to send file via CURL. 您还可以在此处找到良好的来源Curl File Upload,以通过CURL发送文件。 Make sure that you are passing your file on file_contents key in above code. 确保在上述代码中的file_contents键上传递文件。

This is what I did in alignment to 's answer: 这是我根据的回答

$data['type1'] = new CurlFile($file1);
$data['type2'] = new CurlFile($file2);
$data['type3'] = new CurlFile($file3);

//New Code Added
$data['var1'] = "$val1";
$data['var2'] = "$val2";

//removed the trailing string
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM