簡體   English   中英

PHP的卷曲發送文件與數據

[英]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'); 從苗條的框架。 它是空的。

  • 我可以從Header獲取$app->request()->headers('headkey'); headkey $app->request()->headers('headkey');
  • 我可以在$ _FILES中獲取數據

這是由於所有使用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鍵上傳遞文件。

這是我根據的回答

$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