-1

我尝试使用以下 API 对音频文件进行自动调用:https ://contact-everyone.orange-business.com/api/docs/guides/index.html?shell#cr-er-une-diffusion-vocale

'linux cURL' 代码如下(在上一个链接的右侧):

    curl -X POST https://[SERVER_URL]/api/v1.2/groups/[id_group]/diffusion-requests \
-H 'Authorization: Bearer [Access-Token]' \
-H 'Content-Type: multipart/form-data' \
-F audio-intro=@/path/to/myintro.wav \
-F audio-body=@/path/to/mybody.wav \
-F audio-outro=@/path/to/myoutro.wav \
-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 中。我尝试了很多这样的事情:

    $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 网络中的内部路由错误。

我使用“shell_exec($cmd)”在php中使用此服务,其中$cmd是一个原始的bash curl命令,它工作正常。

4

2 回答 2

0

大致-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" 
                ) 
        ) ) ) 
) );
于 2018-09-05T18:55:50.997 回答
0

谢谢@hanshenrik。按照您的指示,我获得以下差异:

- 标题差异(内容类型):
¤ curl = application/json ;
¤ php-curl = multipart/form-data

- 内容差异:
¤ curl = Content-Disposition: form-data; name="audio-body"; filename="Test.wav"
¤ php-curl = Content-Disposition: form-data; name="audio-body"; filename=""

- 内容类型:
¤ curl = Content-Type: application/octet-stream
¤ php-curl = Content-Type:

- 'audio-outro' 属性也有同样的问题:没有文件也没有内容类型。

- 最后,msisdns 值不是同一类型(易于纠正,已经尝试过):
¤ curl = "msisdns":["0612327745"]
¤ php-curl ="msisdns":[["0612345678"]]

如果我将 msisdns 更正为一个暗淡的数组,那么我会通过调用 API 得到以下错误(文件丢失):[{"code":"NotNull","message":"A field is missing or blank, ensure that all mandatory fields are specified."}]

问题似乎来自添加文件的方式。有什么建议吗?

[编辑] @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 
    ) );
}
于 2018-09-11T13:21:22.193 回答