libcurl http post将数据发送到服务器

时间:2015-08-17 08:21:00

标签: c++ c libcurl intel-edison

curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1:8081/get.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"pulse=70 & temp=35" );

以上代码运行成功,但是当我通过此

int pulsedata = 70;
int tempdata  = 35;

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "pulse=pulsedata & temp = tempdata");

当我在上面运行这行时,它会给我错误 我怎么能通过这个pulsedata和tempdata ??

2 个答案:

答案 0 :(得分:0)

你不能在这样的字符串中使用变量,你必须格式化字符串。

可能的C ++解决方案可能是这样使用std::ostringstream

std::ostringstream os;
os << "pulse=" << pulsedata << "&temp=" << tempdata;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, os.str().c_sr());

使用此解决方案,{CURR调用全部完成后,std::ostringstream对象(我的示例中为os)需要处于活动状态。

另请注意,我构造的查询字符串不包含任何空格。

答案 1 :(得分:0)

可能的C解决方案:

char sendbuffer[100];
snprintf(sendbuffer, sizeof(sendbuffer), "pulse=%d&temp=%d", pulsedate, tempdata);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, sendbuffer);