组合字符串不适用于C ++中的libCurl

问题描述 投票:0回答:1

[基本上,我正在使用的程序/代码正在从PHP脚本返回消息。我遇到的问题是当我给它提供URL的组合字符串时,它不返回任何内容。但是,当我手动输入时,它会返回正确的信息。这是我在下面使用的代码:

std::string Login(std::string uname, std::string pass)
{
    CURL* curl;
    CURLcode res;
    std::string readBuffer;
    std::string path = "localhost/files/login.php?username=" + uname + "&password=" + pass;
    std::cout << uname << "  " << pass;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "localhost/files/login.php?username=123&password=123");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        return readBuffer;
    }
    return "Failed";

}

此方法有效,因为我手动输入了URL,但执行此操作不会返回任何内容:

std::string path = "localhost/files/login.php?username=" + uname + "&password=" + pass;

curl_easy_setopt(curl, CURLOPT_URL, path);

我不确定我使用的是错误的变量还是其他。我不熟悉使用PHP和libCurl以及任何与Web相关的内容。

c++ libcurl
1个回答
0
投票

CURLOPT_URL期望输入C样式的以空字符结尾的CURLOPT_URL字符串指针,而不是char*。您可以使用std::string方法从std::string获取兼容的std::string::c_str()指针:

std::string::c_str()
© www.soinside.com 2019 - 2024. All rights reserved.