大致-X POST
翻译为CURLOPT_POST=>1
(实际上确切的翻译将使用 CURLOPT_CUSTOMREQUEST,但不要使用它,而是使用 CURLOPT_POST。)
https://[SERVER_URL]/api/v1.2/groups/[id_group]/diffusion-requests
翻译成CURLOPT_URL => 'https://[SERVER_URL]/api/v1.2/groups/[id_group]/diffusion-requests'
这
-H 'Authorization: Bearer [Access-Token]' \
翻译成
CURLOPT_HTTPHEADER=>array('Authorization: Bearer [Access-Token]')
至于-H 'Content-Type: multipart/form-data'
-根本不要手动添加该标题,curl会为您完成。(如果你手动添加它,你可能会弄乱边界字符串,完整的标题看起来像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 \
翻译成
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"),
)
但接下来的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'
是有问题的,php 的 curl_api 包装器不支持在multipart/form-data
请求的各个参数中添加标头,但是如果幸运的话,您可以在没有Content-Type
标头的情况下进行到期,因此除了标头之外,它会转换为
/*...,*/
"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")
)
));
简而言之:
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"
)
) )
)
) );
编辑:如果你绝对必须有标题,那么你不能使用 PHP 的 curl_api 的 multipart/form-data 生成器,你必须自己滚动,见https://bugs.php.net/bug.php?id=76847 - 这里是一个相当未经测试的例子:
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
) );
}
有了这个,你的 curl 参数将转化为,简而言之:
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"
)
) ) )
) );