C API provides two APIs in the libcurl function. The easy interface is a simple synchronous API (meaning that when you use a request to call libcurl, it will be able to meet your request until it is completed or an error occurs ). Multiple Interfaces further control the libcurl. Your application can perform multiple synchronous transmission operations and control the time and place when libcurl moves data.
This example uses the easy interface. This API can also control the data movement process (using callback), but it is very simple to use, as shown in its name. Listing 3 provides an http c language example.
Listing 3. c HTTP client using libcurl easy interface
#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; } |
The top is requiredinclude
File, including the curl root file. Next, I defined two variables for transmission. The first variable iswr_buf
Indicates the buffer in which incoming data is written.wr_index
Indicates the index currently written to the buffer.
Gomain
Function, which uses easy API to set. All curl calls pass {
Sendmsg ('PW _ Ajax. php', 'Action = relatetag & tagname = maintain', this. ID)
} "> Maintain the handle of a specific request status. This is calledCURL
Pointer Reference. This example also creates a special return code calledCURLcode
. Before using any libcurl function, you must callcurl_easy_init
ObtainCURL
Handle. Next, note thatcurl_easy_setopt
Number of calls. They configure handles for specific operations. For these calls, you provide handles, commands, and options. First, this example usesCURLOPT_URL
Specify the URL to be obtained. Then, it usesCURL_WRITEDATA
Provides a context variable (in this example, it is an internal write error variable ). Finally, it usesCURLOPT_WRITEFUNCTION
Specifies the function to be called when data is available. After the API is started, the API uses the data it reads to call the function multiple times.
To start transmission, callcurl_easy_perform
. It performs transmission according to the previous configuration. When this function is called, the function will not return until the transfer is completed or an error occurs.main
The last step is to submit the return status, read the submission page, and usecurl_easy_cleanup
Clear (after the operation is completed using the handle ).
Now lookwrite_data
Function. This function is called when a specific operation receives data. Note that when you read data from a website (write_data
). A buffer (including available data), number of members, size (total amount of available data in the buffer), and context pointer will be provided to the callback. The first task is to ensure that the buffer (wr_buf
{
Sendmsg ('PW _ Ajax. php', 'Action = relatetag & tagname = space', this. ID)
} "> Enough space to write data. If not, it sets the context pointer and returns 0, indicating a problem. Otherwise, it copies the data in the curl buffer to your buffer and adds an index pointing to the next location to be written. In this example, the string is also terminated and can be used later.printf
. Finally, it returns the number of bytes for the libcurl operation. This will tell the libcurl data to be extracted, and it can also discard the data. This is a relatively simple way to read files from the website to the memory.