Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将libcurl与GoogleSpeechAPI结合使用(数据二进制或上传文件的等价物是什么)?_C++_Curl_Libcurl_Speech To Text - Fatal编程技术网

C++ 如何将libcurl与GoogleSpeechAPI结合使用(数据二进制或上传文件的等价物是什么)?

C++ 如何将libcurl与GoogleSpeechAPI结合使用(数据二进制或上传文件的等价物是什么)?,c++,curl,libcurl,speech-to-text,C++,Curl,Libcurl,Speech To Text,我已经在命令行中找到了实现方法: curl -X POST --upload-file audio/test.wav --header "Content-Type: audio/l16; rate=44100;" "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=MYKEY" 或 其中“MYKEY”是我的开发者密钥 如何在C++中用LIbCURL实现这一点?我已经搜索了数小时

我已经在命令行中找到了实现方法:

curl -X POST --upload-file audio/test.wav 
--header "Content-Type: audio/l16; rate=44100;" 
"https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=MYKEY"

其中“MYKEY”是我的开发者密钥

如何在C++中用LIbCURL实现这一点?我已经搜索了数小时,但无法确定如何使用CURLOPT_POSTFIELDS(或其他参数)将音频文件正确附加到post请求

这是我到目前为止所拥有的。它因访问冲突而中断

#include <iostream>
#include <stdio.h> 
#include <curl/curl.h> 

int main(void)
{
    CURL *curl;         // curl handle
    CURLcode res;

    curl = curl_easy_init();
    if (curl) 
    {
        struct curl_slist *chunk = NULL;

        chunk = curl_slist_append(chunk, "Content-Type: audio/l16; rate=44100");

        std::cout << curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "file=@audio/test.wav") << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 177644) << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk) << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_CAINFO, "ca-bundle.crt") << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=MYKEY") << std::endl;

        res = curl_easy_perform(curl);

        std::cout << res;

        curl_easy_cleanup(curl);

    }
    return 0;
}
#包括
#包括
#包括
内部主(空)
{
CURL*CURL;//CURL句柄
卷曲编码;
curl=curl_easy_init();
if(curl)
{
struct curl_slist*chunk=NULL;
chunk=curl\u slist\u append(chunk,“内容类型:audio/l16;rate=44100”);

std::cout您需要使用回调来上载数据文件。您可以在这里找到示例:


在本例中,数据二进制是不相关的,因为您不需要对在回调中传递数据的文件执行任何操作,因此它将按原样发布。

感谢您为我指明了正确的方向。我已经成功地完成了示例,并取得了一些效果,我在我的问题中链接了这些内容。值得将示例作为答案,而不是作为一部分关于你的问题。
#include <iostream>
#include <stdio.h> 
#include <curl/curl.h> 

int main(void)
{
    CURL *curl;         // curl handle
    CURLcode res;

    curl = curl_easy_init();
    if (curl) 
    {
        struct curl_slist *chunk = NULL;

        chunk = curl_slist_append(chunk, "Content-Type: audio/l16; rate=44100");

        std::cout << curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "file=@audio/test.wav") << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 177644) << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk) << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_CAINFO, "ca-bundle.crt") << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=MYKEY") << std::endl;

        res = curl_easy_perform(curl);

        std::cout << res;

        curl_easy_cleanup(curl);

    }
    return 0;
}
#include <iostream>
#include <stdio.h> 
#include <curl/curl.h> 
#include <direct.h>
#include <string>

struct WriteThis {
    const char *readptr;
    long sizeleft;
};

// callback function from http://curl.haxx.se/libcurl/c/post-callback.html
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{
    struct WriteThis *pooh = (struct WriteThis *)userp;

    if (size*nmemb < 1)
        return 0;

    if (pooh->sizeleft) {
        *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
        pooh->readptr++;                 /* advance pointer */
        pooh->sizeleft--;                /* less data left */
        return 1;                        /* we return 1 byte at a time! */
    }

    return 0;                          /* no more data left to deliver */
}

int main(void)
{
    CURL *curl;         // curl handle
    CURLcode res;

    curl = curl_easy_init();
    if (curl) 
    {
        FILE *file;
        errno_t err = fopen_s(&file, "testaudio.wav", "r");
        fseek(file, 0, SEEK_END);
        int fileSize = ftell(file);
        fseek(file, 0, SEEK_SET);

        std::cout << "file open status: " << err << std::endl;
        std::cout << "file " << fileSize << std::endl;

        struct curl_slist *chunk = NULL;
        chunk = curl_slist_append(chunk, "Content-Type: audio/l16; rate=44100");

        char *audioData = (char*)malloc(fileSize);
        struct WriteThis pooh;
        fread(audioData, fileSize, 1, file);
        fclose(file);

        pooh.readptr = audioData;
        pooh.sizeleft = fileSize;

        std::string sizeHeader = "Content-Length: ";
        sizeHeader += std::to_string(fileSize);
        chunk = curl_slist_append(chunk, sizeHeader.c_str());

        std::cout << curl_easy_setopt(curl, CURLOPT_POST, 1L) << std::endl;
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
        curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        std::cout << curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk) << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_CAINFO, "ca-bundle.crt") << std::endl;
        std::cout << curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=yourkey") << std::endl;

        res = curl_easy_perform(curl);
        std::cout << res;
        curl_easy_cleanup(curl);

    }
    return 0;
}