C API 在 libcurl 功能上提供了兩個 API。easy 介面是一個簡單的同步 API(意味著當您使用請求調用 libcurl 時,將能夠滿足您的請求,直到完成或發生錯誤)。多介面可以進一步控制 libcurl,您的應用程式可以執行多個同步傳輸,並控制 libcurl 何時何地移動資料。
該樣本使用 easy 介面。該 API 還能控制資料移動過程(使用回調),但正如其名稱所示,使用起來非常簡單。清單 3 提供了 HTTP 的 C 語言樣本。
清單 3. 使用 libcurl easy 介面的 C HTTP 用戶端
#include <stdio.h> #include <string.h> #include <curl/curl.h> #define MAX_BUF 65536 char wr_buf[MAX_BUF+1]; int wr_index; /* * Write data callback function (called within the context of * curl_easy_perform. */ size_t write_data( void *buffer, size_t size, size_t nmemb, void *userp ) { int segsize = size * nmemb; /* Check to see if this data exceeds the size of our buffer. If so, * set the user-defined context value and return 0 to indicate a * problem to curl. */ if ( wr_index + segsize > MAX_BUF ) { *(int *)userp = 1; return 0; } /* Copy the data from the curl buffer into our buffer */ memcpy( (void *)&wr_buf[wr_index], buffer, (size_t)segsize ); /* Update the write index */ wr_index += segsize; /* Null terminate the buffer */ wr_buf[wr_index] = 0; /* Return the number of bytes received, indicating to curl that all is okay */ return segsize; } /* * Simple curl application to read the index.html file from a Web site. */ int main( void ) { CURL *curl; CURLcode ret; int wr_error; wr_error = 0; wr_index = 0; /* First step, init curl */ curl = curl_easy_init(); if (!curl) { printf("couldn't init curl/n"); return 0; } /* Tell curl the URL of the file we're going to retrieve */ curl_easy_setopt( curl, CURLOPT_URL, "[url]www.exampledomain.com"[/url] ); /* Tell curl that we'll receive data to the function write_data, and * also provide it with a context pointer for our error return. */ curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)&wr_error ); curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data ); /* Allow curl to perform the action */ ret = curl_easy_perform( curl ); printf( "ret = %d (write_error = %d)/n", ret, wr_error ); /* Emit the page if curl indicates that no errors occurred */ if ( ret == 0 ) printf( "%s/n", wr_buf ); curl_easy_cleanup( curl ); return 0; } |
最上方是必需的 include檔案,包括 cURL 根檔案。接下來,我定義了兩個用於傳輸的變數。第一個變數是 wr_buf,表示將在其中寫入傳入資料的緩衝區。wr_index表示緩衝區的當前寫入索引。
轉到 main函數,該函數使用 easy API 進行設定。所有 cURL 調用都通過{
sendmsg('pw_ajax.php','action=relatetag&tagname=維護',this.id)
}">維護特定請求狀態的控制代碼進行操作。這稱為 CURL指標引用。本例還建立一個特殊的返回碼,稱為 CURLcode。在使用任何 libcurl 函數之前,您需要調用 curl_easy_init擷取 CURL控制代碼。接下來,注意 curl_easy_setopt調用的數量。它們為特定的操作配置控制代碼。對於這些調用,您提供控制代碼、命令和選項。首先,本例使用CURLOPT_URL指定要擷取的 URL。然後,它使用 CURL_WRITEDATA提供一個上下文變數(在本例中,它是內部的 write 錯誤變數)。最後,它使用 CURLOPT_WRITEFUNCTION指定資料可用時應該調用的函數。在啟動 API 之後,API 將使用它讀取的資料多次調用該函數。
要開始傳輸,調用 curl_easy_perform。它的工作是根據之前的配置執行傳輸。調用該函數時,在完成傳輸或發生錯誤之前該函數不會返回。main的最後一步是提交返回狀態,提交頁面讀取,最後使用curl_easy_cleanup清除(當使用控制代碼執行完操作後)。
現在看看 write_data函數。該函數是針對特定操作收到資料時調用的回調。注意,當您從網站讀取資料時,將寫入該資料(write_data)。將向回調提供一個緩衝區(包含可用資料)、成員數量和大小(緩衝中可用資料總量)、上下文指標。第一個任務是確保緩衝區(wr_buf)的{
sendmsg('pw_ajax.php','action=relatetag&tagname=空間',this.id)
}">空間足以寫入資料。如果不夠,它將設定上下文指標並返回 0,表示出現問題。否則,它將 cURL 緩衝區的資料複製到您的緩衝區,並增加索引,指向要寫入的下一個位置。本例還終止字串,稍後可以對其使用 printf。最後,它返回 libcurl 操作的位元組數量。這將告訴 libcurl 資料被提取,它也可以丟棄該資料。這就是從網站將檔案讀取到記憶體的相對簡單的方法。