libcurl 是處理收送網路 request、response 的 library,可用來收送 HTTP 的 request 及 response。
libcurl 收到 response 時會 call callback function,由 callback function 處理收到的資料。callback function 可自行撰寫,其 prototype 如下:
1
| size_t func(void *ptr, size_t size, size_t nmemb, void *userdata)
|
- ptr 指向收到的資料
- nmemb 為收到資料的大小
- userdata 可自己定義,我拿它當 return value。
libcurl 的 callback function 分成處理 header 跟 data,用 curl_easy_setopt()
指定處理的 callback function。如果沒有指定 callback function,預設上 data 會被印出來,header 則不處理。
Usage
1. Initialize
1 2 3
| #include <curl/curl.h> CURL* curl = curl_easy_init();
|
2. Set HTTP header data
設 HTTP header,如指定 Content-type:
1 2 3
| struct curl_slist* headers; memset(header, 0, sizeof(headers)); headers = curl_slist_append(headers, "Content-type: application/json");
|
call curl_slist_append()
會不斷加資訊到 headers 這個資料結構中。
3. Set options
1 2 3 4 5 6 7 8 9 10
| curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parseHeader); curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &rescode);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getResponse); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
4. 傳送 request
1
| curl_easy_perform(curl);
|
以 blocking 的方式傳送 request。
5. clean up
1 2
| curl_slist_free_all(headers); curl_easy_cleanup(curl);
|