cocos2dx-3.3 网络编程(CURL+PHP) NO.2 登陆功能

(提前声明一下,以下内容中passward是错误的,应该是password尴尬


首先应该在头文件加以下内容:

#include "curl/include/win32/curl/curl.h"//网络连接-1
#pragma comment ( lib, "libcurl_imp.lib" )
#pragma comment ( lib, "ws2_32.lib" )
#pragma comment ( lib, "wldap32.lib" )
static size_t  returnData(char *ptr, size_t  size, size_t  nmemb, std::string *stream);//获取数据时的回调函数


在按钮的回调函数里加入:

//-----网络连接-1
CURL* curl = curl_easy_init(); //1 curl初始化
char url[1000] = {0};    
//我们根据用户输入的用户名和密码拼出请求url            
sprintf(url, "http://127.0.0.1/check.php?name=%s&passward=%s",m_name.c_str(), m_password.c_str());  
int res;  
    //2 网络连接初始化  
    res = curl_easy_setopt(curl, CURLOPT_URL, url);  //设置要连接的网址, res返回0表示成功  
    //3 设定数据接收方法  
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, returnData);  
    //4 设定数据接收变量  
    std::string recvbuf;  
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &recvbuf);  
    //5 发起联网请求  
    res = curl_easy_perform(curl);  
    //6 处理结果,根据网络连接返回的结果实现跳转和提示  
CCLOG(url);
    if (CURLE_OK == res)  //CURLE_OK == 0  
    {  
char log_msg[1000];
sprintf(log_msg,recvbuf.c_str());
CCLOG("-----return Data start");
CCLOG(log_msg);
CCLOG("-----return Data end");
        if (recvbuf.compare("1")==1)  //如果返回结果为1,即用户名和密码匹配上  
        {  
            CCLOG("login success");
        }  
        else  //否则登录失败  
        {  
            CCLOG("login failed");
        }  
  
    }  

 以下是回调函数: 

size_t  returnData(char *ptr, size_t  size, size_t  nmemb, std::string *stream)  
{  
    //char* ptr就是返回的服务器数据,服务器echo 1,这里就返回"1"  
    CCLOG("is writing");  
    if (stream == NULL)  
    {  
        return 0;  
    }  
    size_t  sizes = size * nmemb;  
    //string* ss = (string *)stream;  
    stream->append(ptr, sizes);  
    return sizes;  
}  


对于curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, returnData)中的CURLOPT_WRITEFUNCTION,官方有如下解释:

Pass a pointer to your callback function, which should match the prototype shown above.

This callback function gets called by libcurl as soon as there is data received that needs to be saved. ptrpoints to the delivered data, and the size of that data is size multiplied with nmemb.

The callback function will be passed as much data as possible in all invokes, but you must not make any assumptions. It may be one byte, it may be thousands. The maximum amount of body data that will be passed to the write callback is defined in the curl.h header file: CURL_MAX_WRITE_SIZE (the usual default is 16K). If CURLOPT_HEADER is enabled, which makes header data get passed to the write callback, you can get up to CURL_MAX_HTTP_HEADER bytes of header data passed into it. This usually means 100K.

This function may be called with zero bytes data if the transferred file is empty.

The data passed to this function will not be zero terminated!

Set the userdata argument with the CURLOPT_WRITEDATA option.

Your callback should return the number of bytes actually taken care of. If that amount differs from the amount passed to your callback function, it'll signal an error condition to the library. This will cause the transfer to get aborted and the libcurl function used will return CURLE_WRITE_ERROR.

If your callback function returns CURL_WRITEFUNC_PAUSE it will cause this transfer to become paused. See curl_easy_pause for further details.

Set this option to NULL to get the internal default function used instead of your callback. The internal default function will write the data to the FILE * given with CURLOPT_WRITEDATA.

第一次硬着头皮啃e文,发现想要理解还是相当有难度的,遇到不懂的单词还要翻译一下 尴尬


好了,我们现在测试一下:

输入正确


输出错误



成功咯


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值