Requirement to send Form data/file to another external API from A PHP server ?. Imagine a story to upload an image to an external file upload API from a PHP server( Can be a CMS like WordPress).
This task can be achieved using CURL. PHP now has a class CurlFile which is now is available in the PHP versions PHP 5 >= 5.5.0, PHP 7. The following is the snippet to use CurlFile.
$url = "remote api url";
$filename = $_FILES['file']['name'];
$filedata = $_FILES['file']['tmp_name'];
$filetype = $_FILES['file']['type'];
if ($filedata != '')
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Secret: if there is secret',
'key: if there is key'
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$args['file'] = new CurlFile($filedata , $filetype, $filename);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$errmsg = "Please chose a file";
You can use Ajax to get the file and send it using the above code. Modify the code to handle the errors and line
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0)
should be avoided in production as SSL verification is ignored with this option.