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文,發現想要理解還是相當有難度的,遇到不懂的單詞還要翻譯一下尷尬


好了,我們現在測試一下:

輸入正確


輸出錯誤



成功咯


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章