Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maitret/2877b8b6ad73f46d906aa4055c4ae621 to your computer and use it in GitHub Desktop.
Save maitret/2877b8b6ad73f46d906aa4055c4ae621 to your computer and use it in GitHub Desktop.
PHP cURL request multipart/form-data #php #curl #request #multipart #formdata #file #upload
<?php

$ch = curl_init();

$headers   = array();
$headers[] = "Token: " . $apiToken;
//$headers[] = "Content-Type: multipart/form-data";  // ERROR: no usen esta cabecera. Si $post es un array, la cabecera se añade automáticamente. Si se añade manualmente, el boundary y el base64 se genera mal.

$cfile = new \CURLFile($fileNameWithFullPath);

//$post = array('image' => '@/tmp/phpDT8j');         // ERROR: no usen '@'. Genera errores en el base64. Desde PHP 5.5 está obsoleto. En su lugar usar CURLFile.
$post = array('image' => $cfile);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
//curl_setopt($ch, CURLOPT_POST, true);

// Send request to Burp Suite (debug)
//curl_setopt($ch, CURLOPT_PROXY, 'localhost:8080');
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result   = curl_exec($ch);
$response = curl_getinfo($ch);
$aa       = curl_multi_getcontent($ch);
curl_close($ch);

return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment