牛骨文教育服务平台(让学习变的简单)
博文笔记

curl CURLOPT_WRITEFUNCTION 的参数设置

创建时间:2014-11-26 投稿人: 浏览次数:4902

注册CSDN有十几年了,第一次发文。写博客不求有没有人看,最大的好处是可以记录一些心得。

最近研究CURL,第一步就卡了一天。代码如下:

string operation="";

curl_easy_setopt(curl,CURLOPT_URL, "http://localhost/index2.php?username=superdos&password=123"); 

curl_easy_setopt(curl,CURLOPT_POST, true); 

curl_easy_setopt(curl,CURLOPT_POSTFIELDS,operation.c_str()); 

curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,&HelloWorld::writeFunction); curl_easy_setopt(curl,CURLOPT_WRITEDATA, &buffer); curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,5000); ret = curl_easy_perform(curl); if(ret==CURLE_OK) { log("ok"); } curl_easy_cleanup(curl);

其中writeFunction的声明: size_t HelloWorld::writeFunction(void* ptr,size_t size,size_t number,void *stream);

调适过程中发现,虽然curl_easy_perform()返回正常,但是writeFunction函数中,输入参数的内容不正常。ptr,stream输入的不是内存地址,而是返回数据的长度及批次。number反而是一个内存地址。

后来想到在模块间传递类成员函数指针,必须是static的,于是在write Function的声明前加上static后正常。

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。