LinuxCurl-F到PHPCurl-联系所有人API
我尝试使用以下API对音频文件进行自动调用: “LinuxCurl”代码如下(在上一个链接的右侧): 我也在搜索,但是用PHP。我尝试过很多类似的事情:LinuxCurl-F到PHPCurl-联系所有人API,curl,php-curl,orange-api,Curl,Php Curl,Orange Api,我尝试使用以下API对音频文件进行自动调用: “LinuxCurl”代码如下(在上一个链接的右侧): 我也在搜索,但是用PHP。我尝试过很多类似的事情: $diffusion_params = '"diffusion"={ "name":"diffusion vocale via API REST", "contactIds":[], "mailingListIds":[], "excludedCon
$diffusion_params = '"diffusion"={
"name":"diffusion vocale via API REST",
"contactIds":[],
"mailingListIds":[],
"excludedContactIds":[],
"msisdns":["0612345678"],
"landlines":[],
"voiceParam":{
"locale": "fr_FR"
}
};type=application/json' ;
$audio_intro_param = '"audio-intro"="@/path/to/file/sound.wav"';
$audio_body_param = '"audio-body"="@/path/to/file/sound.wav"';
$audio_outro_param = '"audio-outro"="@/path/to/file/sound.wav"';
$post_field_params = array($audio_intro_param, $audio_body_param, $audio_outro_param, $diffusion_params);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://'.$URL.'/api/v1.2/groups/'.$group_id.'/diffusion-requests');
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$token, "Content-Type: multipart/form-data") );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, $post_field_params );
$result = curl_exec($ch);// Retourne un résultat de la forme suivante :
curl_close($ch);
我的问题涉及所有“-F”选项。
如何在PHP中“转换”它
[更新]
问题是Orange网络中的内部路由错误
我在php中使用了一个“shell_exec($cmd)”,其中$cmd是一个原始的bash curl命令,它工作得很好。-X POST大致翻译为
CURLOPT_POST=>1
(实际上,准确的翻译应该是CURLOPT_CUSTOMREQUEST,但不要使用它,而是使用CURLOPT_POST。)
https://[SERVER\u URL]/api/v1.2/groups/[id\u group]/diffusion请求
转换为CURLOPT\u URL=>“https://[SERVER\u URL]/api/v1.2/groups/[id\u group]/diffusion请求”
转化为
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"),
)
至于-H'Content-Type:multipart/formdata'
-根本不要手动添加该标题,curl将为您添加该标题。(如果手动添加,可能会弄乱边界字符串,完整标题类似于Content-Type:multipart/form-data;boundary=---------------------------82442bc797f0
)
转化为
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"),
)
但是接下来的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_uuAPI包装器不支持将头添加到多部分/表单数据
请求的各个参数中,但是如果幸运的话,您可以在没有内容类型
头的情况下到期,因此除了该头之外,它将转换为
/*...,*/
"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_uAPI的多部分/表单数据生成器,您必须使用自己的数据生成器,请参见-下面是一个未经测试的示例:
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"
)
) ) )
) );
谢谢你@hanshenrik。按照您的指示,我得到了以下区别:
-标题差异(内容类型):
·curl=
应用程序/json代码>
·php curl=多部分/表单数据
-内容差异:
·curl=内容处理:表单数据;name=“音频体”;filename=“Test.wav”
·php curl=内容配置:表单数据;name=“音频体”;filename=”“
-内容类型:
·curl=内容类型:应用程序/八位字节流
·php curl=内容类型:
-“audio outro”属性也存在同样的问题:没有文件和内容类型
-最后,msisdns值不是同一类型(易于更正,已经尝试过):
·旋度=“msisdns”:[“0612327745”]
·php curl=“msisdns”:[[“0612345678”]
如果我将MSISDN更正为一维数组,则通过调用API(文件丢失)会出现以下错误:[{“code”:“NotNull”,“message”:“字段丢失或为空,请确保指定了所有必填字段”。}]
问题似乎来自添加文件的方式。有什么建议吗
[编辑]
@hanshenrik:我使用以下PHP代码获得这些结果:
function sendCallFromFile($URL, $token, $group_id, $file_path, $my_mobile_phone_number)
{
$ch = curl_init();
curl_setopt_array ( $ch, array (
CURLOPT_URL => 'https://'.$URL.'/api/v1.2/groups/'.$group_id.'/diffusion-requests',
CURLOPT_POST => 1
) );
echo (file_exists($file_path)) ? "The file exists.\n" : "ERROR : The file does not exist !!!\n"; // print "The file exists"
shitty_multipart_form_data_generator ( $ch, array (
'Authorization: Bearer '.$token
), array (
"audio-intro" => new CURLFile ( $file_path ),
"audio-body" => new CURLFile ( $file_path ),
"audio-outro" => new CURLFile ( $file_path ),
"diffusion" => new CURLMultiPart ( array (
'Content-Type: application/json'
), json_encode ( array (
"name" => "diffusion vocale via API REST",
"contactIds" => array ( ),
"mailingListIds" => array (),
"excludedContactIds" => array (),
"msisdns" => array (
$my_mobile_phone_number
),
"landlines" => array (/*"0412345678"*/),
"voiceParam" => array (
"locale" => "fr_FR"
)
) ) )
) );
$result = curl_exec($ch);// Retourne un résultat de la forme suivante :
curl_close($ch);
return $result;
}
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
) );
}
谢谢你的回复。但那不行,我没有接到电话(API错误消息如下所示(无效):“{”logId:“180907W687754048”,“消息”:“}”.@Jeremedem好的,那么就需要单独参数的内容类型头了。我不想这样做,但我知道如何修复它,我只是希望我不必…请在hourHi@hanshenrik中检查,谢谢你的帮助!你的函数名命名正确。^^通过更新的代码,我得到以下错误消息:[{“code”:“NotNull”,“消息”:“字段缺失或为空,请确保指定了所有必填字段。”}]@Jeremedem将curl请求和php libcurl请求重定向到,并保存在2个文件中,然后在一个diff工具中运行它们,它们的区别是什么?谢谢@hanshenrik。按照您的说明,我获得了以下区别您注意到的第一个区别表明您试图放置多部分/表单数据
使用CURLOPT_HTTPHEADER作为php中的全局头-显示您在php中使用的代码第二个差异表明我的CURLFile解析器中存在错误,或者您为CURLFile的$postname
参数指定了空字符串::u>第三个差异(msisdns)建议代码应该是“msisdns”=>数组(“0612327745“
而不是“msisdns”=>阵列(0=>阵列(“0612327745”)
function sendCallFromFile($URL, $token, $group_id, $file_path, $my_mobile_phone_number)
{
$ch = curl_init();
curl_setopt_array ( $ch, array (
CURLOPT_URL => 'https://'.$URL.'/api/v1.2/groups/'.$group_id.'/diffusion-requests',
CURLOPT_POST => 1
) );
echo (file_exists($file_path)) ? "The file exists.\n" : "ERROR : The file does not exist !!!\n"; // print "The file exists"
shitty_multipart_form_data_generator ( $ch, array (
'Authorization: Bearer '.$token
), array (
"audio-intro" => new CURLFile ( $file_path ),
"audio-body" => new CURLFile ( $file_path ),
"audio-outro" => new CURLFile ( $file_path ),
"diffusion" => new CURLMultiPart ( array (
'Content-Type: application/json'
), json_encode ( array (
"name" => "diffusion vocale via API REST",
"contactIds" => array ( ),
"mailingListIds" => array (),
"excludedContactIds" => array (),
"msisdns" => array (
$my_mobile_phone_number
),
"landlines" => array (/*"0412345678"*/),
"voiceParam" => array (
"locale" => "fr_FR"
)
) ) )
) );
$result = curl_exec($ch);// Retourne un résultat de la forme suivante :
curl_close($ch);
return $result;
}
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
) );
}