the -X POST
roughly translates to CURLOPT_POST=>1
(actually the exact translation would be with CURLOPT_CUSTOMREQUEST, but don't use that, use CURLOPT_POST instead.)
the https://[SERVER_URL]/api/v1.2/groups/[id_group]/diffusion-requests
translates to CURLOPT_URL => 'https://[SERVER_URL]/api/v1.2/groups/[id_group]/diffusion-requests'
the
-H 'Authorization: Bearer [Access-Token]' \
translates to
CURLOPT_HTTPHEADER=>array('Authorization: Bearer [Access-Token]')
as for -H 'Content-Type: multipart/form-data'
- don't add that header manually at all, curl will do it for you. (if you add it manually, you may mess up the boundary string, the full header looks something like Content-Type: multipart/form-data; boundary=------------------------82442bc797f0
)
-F audio-intro=@/path/to/myintro.wav \
-F audio-body=@/path/to/mybody.wav \
-F audio-outro=@/path/to/myoutro.wav \
translates to
CURLOPT_POSTFIELDS=>array(
"audio-intro"=>new CURLFile("/path/to/myintro.wav"),
"audio-body"=> new CURLFile("/path/to/mybody.wav"),
"audio-outro"=>new CURLFile("/path/to/myoutro.wav"),
)
but the next 1,
-F 'diffusion={
"name":"diffusion vocale via API REST",
"contactIds":["id_contact_1", "id_contact_2", ...],
"mailingListIds":["id_mailing_list_1","id_mailing_list_2", ...],
"excludedContactIds":[],
"msisdns":["0612327745"],
"landlines":["0522331155"],
"voiceParam":{
"locale": "fr_FR"
}
};type=application/json'
is problematic, php's curl_ api wrapper does not support adding headers to individual parameters of multipart/form-data
requests, but if you're lucky, you can make due without the Content-Type
header, so except that header, it translates to
/*...,*/
"diffusion"=>json_encode(array(
"name"=>"diffusion vocale via API REST",
"contactIds"=>array("id_contact_1", "id_contact_2", ...),
"mailingListIds"=>array("id_mailing_list_1","id_mailing_list_2", ...),
"excludedContactIds"=>array(),
"msisdns"=>array(0=>array("0612327745")),
"landlines"=>array("0522331155"),
"voiceParam"=>array("locale"=>"fr_FR")
)
));
so in short:
curl_setopt_array ( $ch, array (
CURLOPT_URL => 'https://[SERVER_URL]/api/v1.2/groups/[id_group]/diffusion-requests',
CURLOPT_HTTPHEADER => array (
'Authorization: Bearer [Access-Token]'
),
CURLOPT_POSTFIELDS => array (
"audio-intro" => new CURLFile ( "/path/to/myintro.wav" ),
"audio-body" => new CURLFile ( "/path/to/mybody.wav" ),
"audio-outro" => new CURLFile ( "/path/to/myoutro.wav" ),
"diffusion" => json_encode ( array (
"name" => "diffusion vocale via API REST",
"contactIds" => array (
"id_contact_1",
"id_contact_2",
(...)
),
"mailingListIds" => array (
"id_mailing_list_1",
"id_mailing_list_2",
(...)
),
"excludedContactIds" => array (),
"msisdns" => array (
0 => array (
"0612327745"
)
),
"landlines" => array (
"0522331155"
),
"voiceParam" => array (
"locale" => "fr_FR"
)
) )
)
) );
edit: if you absolutely must have the header, then you cannot use PHP's curl_ api's multipart/form-data generator, you must roll your own, see https://bugs.php.net/bug.php?id=76847 - here is a fairly untested example:
class CURLMultiPart {
/** @var string[] $headers */
public $headers;
/** @var string $value */
public $value;
/**
*
* @param string $value
* @param string[] $headers
*/
function __construct(array $headers, string $value) {
// todo: verify that all $headers are strings.
$this->headers = $headers;
$this->value = $value;
}
}
/**
*
* @param curl_resource $ch
* @param string[] $additional_headers
* @param array $post_data
* @throws \InvalidArgumentException
*/
function shitty_multipart_form_data_generator($ch, array $additional_headers = [], array $post_data) {
$bon = '------------------------' . bin2hex ( random_bytes ( 8 ) );
$global_header = 'Content-Type: multipart/form-data; boundary=' . $bon;
$body = '';
foreach ( $post_data as $post_name => $post_value ) {
$body .= "$bon\r\n";
if (is_string ( $post_value )) {
$body .= "Content-Disposition: form-data; name=\"$post_name\"\r\n";
$body .= "\r\n$post_value\r\n";
} elseif (is_a ( $post_value, 'CURLMultiPart', false )) {
/** @var CURLMultiPart $post_value */
$has_content_disposition = false;
foreach ( $post_value->headers as $header ) {
if (0 === stripos ( $header, 'Content-Disposition' )) {
$has_content_disposition = true;
break;
}
}
if (! $has_content_disposition) {
$body .= "Content-Disposition: form-data; name=\"$post_name\"\r\n";
}
foreach ( $post_value->headers as $header ) {
$body .= "$header\r\n";
}
$body .= "\r\n{$post_value->value}\r\n";
} elseif (is_a ( $post_value, 'CURLFile' )) {
/** @var CURLFile $post_value */
// Content-Disposition: form-data; name="file"; filename="myPostName"
// Content-Type: myMime
$body .= "Content-Disposition: form-data; name=\"$post_name\"; filename=\"" . $post_value->getPostFilename () . "\"\r\n";
$body .= "Content-Type: " . $post_value->getMimeType () . "\r\n\r\n";
$body .= file_get_contents ( $post_value->getFilename () );
$body .= "\r\n";
} else {
// error, invalid argument.
ob_start ();
var_dump ( [
$post_name => $post_value
] );
$debug = ob_get_clean ();
throw new \InvalidArgumentException ( "every member of \$post_data must be either a string, CURLMultiPart, or CURLFile - but contains something else: " . $debug );
}
// unreachable
}
$body .= "{$bon}--\r\n";
// var_dump ( $body );
$additional_headers [] = $global_header;
curl_setopt_array ( $ch, array (
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $additional_headers
) );
}
with this, your curl arguments would translate to, in short:
curl_setopt_array ( $ch, array (
CURLOPT_URL => 'https://[SERVER_URL]/api/v1.2/groups/[id_group]/diffusion-requests',
CURLOPT_POST => 1
) );
shitty_multipart_form_data_generator ( $ch, array (
'Authorization: Bearer [Access-Token]'
), array (
"audio-intro" => new CURLFile ( "/path/to/myintro.wav" ),
"audio-body" => new CURLFile ( "/path/to/mybody.wav" ),
"audio-outro" => new CURLFile ( "/path/to/myoutro.wav" ),
"diffusion" => new CURLMultiPart ( array (
'Content-Type: application/json'
), json_encode ( array (
"name" => "diffusion vocale via API REST",
"contactIds" => array (
"id_contact_1",
"id_contact_2"
// (...)
),
"mailingListIds" => array (
"id_mailing_list_1",
"id_mailing_list_2"
// (...)
),
"excludedContactIds" => array (),
"msisdns" => array (
0 => array (
"0612327745"
)
),
"landlines" => array (
"0522331155"
),
"voiceParam" => array (
"locale" => "fr_FR"
)
) ) )
) );