php - Passing input file from form to cURL POST -
i have form file upload field posts endpoint of mine. there, i'd post file api use.
what's best way of achieving this?
do have move file temporary storage in order post api? or maybe have 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 $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); } );
i took @barmar's advice , created curlfile.
$f3->route('post /my_endpoint', function($f3) { $url = api_endpoint; $post_params = $f3->get('post'); $files_params = $f3->get('files'); $resume_file = curl_file_create(realpath($files_params['resume']['tmp_name']),$files_params['resume']['type'],$files_params['resume']['name']); $fields = array( 'id' => $post_params['id'], 'email' => $post_params['email'], 'resume' => $resume_file ); $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); $result = curl_exec($ch); curl_close($ch); } );
and worked.
Comments
Post a Comment