500 使用 AcrCloud 上传音频文件时出错 RESTful api php

500 Error uploading An Audio File Using AcrCloud RESTful api php

我正在尝试使用 php 上传音频文件 我知道了 我在 Postman 中尝试了相同的凭据并且它有效。 我找不到问题所在

我正在关注 ACRCloud 文档 here

  HTTP/1.1 100 Continue
    < HTTP/1.1 500 Internal Server Error
    < Server: openresty/1.9.7.4
    < Date: Mon, 23 Jan 2017 16:51:29 GMT
    < Content-Type: application/json; charset=UTF-8
    < Transfer-Encoding: chunked
    < Connection: keep-alive
    < X-Powered-By: PHP/5.6.21
    < X-Rate-Limit-Limit: 600
    < X-Rate-Limit-Remaining: 599
    < X-Rate-Limit-Reset: 0

    HTTP error before end of send, stop sending
    <
    Closing connection 0`

我的代码

$request_url = 'https://api.acrcloud.com/v1/audios';
$http_method = 'POST';
$http_uri = '/v1/audios';
$timestamp = time();
$signature_version = '1';
$account_access_key = '';
$account_access_secret = '';
$string_to_sign =
$http_method . "\n" .
$http_uri . "\n" .
$account_access_key . "\n" .
$signature_version . "\n" .
$timestamp;
$signature = base64_encode(hash_hmac("sha1", $string_to_sign, $account_access_secret, true));
$realpath = realpath('uploads/*****.mp3');

    if(class_exists('\CURLFile'))
        $cfile = new \CURLFile($realpath, "audio/mp3", basename($realpath));
    else
        $cfile = '@' . $realpath;
    $fields = array(
        'audio_id' => '30007',
        'title' => 'aya number 19',
        'audio_file' => $cfile,
        'bucket_name' => 'whatever',
        'data_type' => 'audio',  // if you upload fingerprint file please set 'data_type'=>'fingerprint'
        'custom_key[0]' => 'track_id',
    );
    $headerArray = array();
    $headers = array(
        'access-key' => $account_access_key,
        'signature' => $signature,
        'signature-version' => '1',
        'timestamp' => $timestamp,
    );
    foreach( $headers as $n => $v ) {
        $headerArr[] = $n .':' . $v;
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $request_url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $verbose = fopen('php://temp', 'w+');
    curl_setopt($ch, CURLOPT_STDERR, $verbose);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
    $result = curl_exec($ch);
    if ($result === FALSE) {
        printf("cUrl error (#%d): %s<br>\n", curl_errno($ch),
            htmlspecialchars(curl_error($ch)));
    }
    rewind($verbose);
    $verboseLog = stream_get_contents($verbose);

    echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
    dd($result);
    curl_close($ch);

我发现了:

1- 你必须使用本地文件

2-curl取绝对文件路径

3- 如果使用 custom_key[0] 后必须添加 custom_value[0]

感谢支持,现在可以使用了

希望对某人有所帮助