I have a form with a file upload field which POSTs to an endpoint of mine. From there, I'd like to POST the file to an API I use.
What's the best way of achieving this?
Do I have to move the file from temporary storage in order to POST it to the API? Or maybe I have to add cURL options?
HTML:
<form method="post" action="/my_endpoint">
<div class="form-group">
<label for="resume">Resume*</label>
<input type="file" class="form-control" id="resume" name="resume" placeholder="Resume" required />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
PHP:
$f3->route('POST /my_endpoint',
function($f3) {
$url = API_ENDPOINT;
$post_params = $f3->get('POST');
$files_params = $f3->get('FILES');
$fields = array(
'id' => $post_params['id'],
'email' => $post_params['email'],
'resume' => '@'.$files_params['resume']['tmp_name']
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
}
);