1

I need to upload 3 files along with variables in post data. 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'); from slim framework. It is empty.

  • I am able to get the headkey from the Header as $app->request()->headers('headkey');
  • I am able to get the data in $_FILES

3 Answers 3

2

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. Make sure that you are passing your file on file_contents key in above code.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks looks like this worked. I was using some variables with the $data array, everything should go in the $data array then the _POST and _FILES resolves properly.
yes absolutely , so here instead of post will be data
You have different options to submit multidimensional data. Either you can submit json string by converting you data or break that in key value pair which can be transmitted as http data.
2

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

1 Comment

yes its the same passing to array simply and this will be work with server side.
1

It's due to the fact that all files uploaded with HTTP POST method are in $_FILES global variable. That's why you cannot access files by this way

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

but you can by using $_FILES

$_FILES description

An associative array of items uploaded to the current script via the HTTP POST method. The structure of this array is outlined in the POST method uploads section.

2 Comments

@SSingh I've copied the code from author's post. I don't know why I got -1 if my answer is correct :).
Thanks for pointing out the error, I have corrected it. However I cannot change the server side code. I need to create the correct php curl request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.