[英]C++ Curl post dynamic var
I would like to use dynamic variables with POST curl 我想在POST curl中使用动态变量
I use this code: 我使用以下代码:
int send(const char*s)
{
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/query.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "q=" + s);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
std::cout << std::endl << "Query sent" << std::endl;
return 0;
}
And i get this error: 我得到这个错误:
test.cpp:199:57: error: invalid operands of types ‘const char [3]’ and ‘const char*’ to binary ‘operator+’
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "q=" + s);
~~~~~^~~
You have to concatenate "q="
and s
by yourself, there is no operator +
in Cpp which concatenates chars array with pointer to chars. 您必须自己连接"q="
和s
,Cpp中没有运算符+
可以将chars数组与指向chars的指针连接起来。 Create string with "q="
, add data pointed by s
to this string and call c_str()
to get const char*
pointer as parameter of curl_easy_setopt function: 使用"q="
创建字符串,将s
指向的数据添加到此字符串,然后调用c_str()
以获取const char*
指针作为curl_easy_setopt函数的参数:
#include <string>
....
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/query.php");
std::string buf("q=");
buf += s;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf.c_str());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.