Curl C/C ++ API usage routine

Source: Internet
Author: User
Tags ftp site

From: http://blog.csdn.net/mr_von/article/details/3326728

Summarize the articles related to curl on this site:

Install PHP extension curl in Linux
Curl Chinese User Manual (1)
Curl Chinese User Manual (2)
Curl Chinese User Manual (III)

[Post] PHP curl Function Extension document
Example of PHP curl Extension
Use curl to download large files

In addition to the above curl, it also provides a set of Network C/C ++ API interfaces. The following is a source code routine:

Source: http://bbs.chinaunix.net/viewthread.php? Tid = 586014

Two days ago, I saw someone asking the client socket to send the http package code. I was inspired by the FLW Moderator to find some Perl materials, but I am still not familiar with Perl. There is no in-depth research. The library libcurl. So was accidentally discovered. Search on Google to find that it is the library that processes the HTTP request sent by the client and the package that can process the web server's return. I studied the results of the study in two days and shared them with everyone for research.

Reference: http://curl.haxx.se/This is the home page for Curl developers.

Using the libcurl. So library, we can easily connect to a Web site. Obtain the HTML code of a homepage or the header of an HTTP request. You can also submit a form,
It also supports FTP, https,

/Usr/include/curl. h.

1 curlcode curl_global_init (long flags );

Description:
This function can be used only once. (Actually, it can be used again after calling the curl_global_cleanup function)
If this function is not called when the curl_easy_init function is called, it is automatically completed by the libcurl library.

Parameter: flags

Curl_global_all // Initialize all possible calls.
Curl_global_ssl // supports the Secure Socket Layer during initialization.
Curl_global_win32 // initialize the Win32 socket library.
Curl_global_nothing // No additional initialization.

2 void curl_global_cleanup (void );

Description: Used to clear curl_global_init when libcurl is used. Similar to the close function.

3 char * curl_version ();

Description: The version of the current libcurl library.

4 curl * curl_easy_init ();

Description:
Curl_easy_init is used to initialize a curl pointer (some are the same as returning a file pointer). The curl_easy_cleanup function should be used to clean up at the end of the call.
Generally, curl_easy_init indicates the start of a session. Its return values are generally used in functions of the easy series.

5 void curl_easy_cleanup (curl * handle );

Description:
This call ends a session. It is used with curl_easy_init.

Parameters:
Curl type pointer.

6 curlcode curl_easy_setopt (curl * handle, curloption option, parameter );

Description: This function is the most important. Almost all curl programs need to use it frequently.
It tells the curl library how the program will behave, such as viewing the HTML code of a webpage.
(This function is somewhat like the ioctl function)

Parameters:
1 curl type pointer
2. Various options of curloption type. (They are all defined in the curl. h library. Man can also view them)
3 parameter this parameter can be a function pointer, a pointer to an object, or a long variable. What it uses depends on the second parameter.

Curloption has many values. For details, refer to the man manual.

7 curlcode curl_easy_perform (curl * handle );

Description: This function is called after the curl type pointer is initialized and the curl_easy_setopt function is complete. It literally means that the perform is like a stage. Let's set
Option operation.

Parameters:
Curl type pointer.

Here is a simple example:
HTML code used to obtain a home page

# Include <stdio. h >;# include <curl/curl. h >;# include <stdlib. h>; int main (INT argc, char * argv []) {curl * curl; // defines the curl type pointer curlcode res; // define the variable if (argc! = 2) {printf ("Usage: file <URL>;/N"); exit (1) ;}curl = curl_easy_init (); // initialize a curl type pointer if (curl! = NULL) {// set the curl option. curlopt_url specifies the URL. curl_easy_setopt (curl, curlopt_url, argv [1]); // call curl_easy_perform to execute our settings. and perform related operations. it is displayed only on the screen. res = curl_easy_perform (curl); // clear the curl operation. curl_easy_cleanup (curl);} return 0 ;}

Compilation: gcc-O 001-wall 001.c-lcurl

Let's get the HTML code of the www.chinaunix.net homepage.

./001 www.chinaunix.net

Let's look at an example:
In actual programming, we may not show it only. Our purpose is to process HTML code, such as checking keywords and discovering important information.

Then we need to save the obtained HTML code to the corresponding file. See the following example.

# Include <stdio. h >;# include <stdlib. h >;# include <unistd. h >;# include <curl/curl. h >;# include <curl/types. h >;# include <curl/easy. h>; file * FP; // defines the file type pointer size_t write_data (void * PTR, size_t size, size_t nmemb, void * stream) // This function is used to conform to curlopt_writefunction, the constructed {int written = fwrite (PTR, size, nmemb, (File *) FP); Return written;} int main (INT argc, char * argv []) {curl * curl; curl_global_init (curl_global_all); curl = curl_easy_init (); curl_easy_setopt (curl, curlopt_url, argv [1]); If (FP = fopen (argv [1], "W") = NULL) {curl_easy_cleanup (curl); exit (1);} curl_easy_setopt (curl, curlopt_writefunction, write_data ); // curlopt_writefunction hand over the subsequent action to the write_data function to process curl_easy_perform (curl); curl_easy_cleanup (curl); exit (0 );}

Gcc-O 002-wall 002.c-lcurl
./002 www.chinaunix.net
In this example, the HTML code is saved in the www.chinaunix.net file.

In addition, you can also obtain the header post form of the HTTP packet, and so on. Here we will not detail it here. The specific can be man curl_easy_setopt.
(An important struct, httppost)

The following is an example of downloading files from an FTP site.

# Include <stdio. h >;# include <curl/curl. h >;# include <curl/types. h >;# include <curl/easy. h>; struct ftpfile // defines a structure to pass to the my_fwrite function. use the curlopt_writedata option of curl_easy_setopt to pass {char * filename; file * stream;}; int my_fwrite (void * buffer, size_t size, size_t nmemb, void * Stream) {struct ftpfile * out = (struct ftpfile *) stream; // The stream pointer is actually the IF (Out &&! Out->; stream) {out->; stream = fopen (out->; filename, "WB "); // if this stream does not exist, create an out->; filename. if (! Out->; stream) Return-1;} return fwrite (buffer, size, nmemb, out->; stream);} int main (INT argc, char * argv []) {curl * curl; curlcode res; struct ftpfile = {argv [2], null}; // Initialize an ftpfile structure curl_global_init (curl_global_default); curl = curl_easy_init (); if (curl) {curl_easy_setopt (curl, curlopt_url, argv [1]); curl_easy_setopt (curl, curlopt_writefunction, my_fwrite); curl_easy_setopt (curl, curlopt _ Writedata, & ftpfile); // transmits a struct pointer curl_easy_setopt (curl, curlopt_verbose, true) to the fourth parameter of the relevant function ); // curlopt_verbose is often used to display the information returned by server-related operations on the screen. Res = curl_easy_perform (curl); curl_easy_cleanup (curl); If (curle_ OK! = Res) fprintf (stderr, "curl told us % d/N", Res);} If (ftpfile. stream) fclose (ftpfile. stream); curl_global_cleanup (); Return 0 ;}

Gcc-O 003-wall 003.c-lcurl
./003 ftp: // 202.96.64.144/fei.gif

I have an anonymous FTP website that saves the fei.gif file in the directory to a local file, also called fei.gif.

In addition, the curl_escape curl_unescape function is used to convert Chinese characters to the "% xx" type and to convert them back. If you want to download a file with Chinese characters, you must first call the function to convert the string.

The curl library has many other functions to be explored.

Note: Add-lcurl during GCC compilation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.