Curl/C + + API interface usage routines

Source: Internet
Author: User
Tags ftp site

The first two days to see someone asked the client socket to send HTTP packet code, by FLW Moderator inspired to find some Perl information, but not very familiar with Perl. There is no further research. Inadvertently discovered the libcurl.so this library. Go to Google search found that it is processing the client sends HTTP requests to the library and can process the Web server loopback back of the package. We studied the results of the study for two days and shared it with all of us.

Reference: http://curl.haxx.se/This is the first page of the Curl developer.

With the Libcurl.so library we can easily connect to a Web site. Get the HTML code for a home page or the header of an HTTP request. You can also submit a form,
In addition it supports FTP,HTTPS,

The/usr/include/curl/curl.h.

1 Curlcode curl_global_init (long flags);

Describe:
This function can only be used once. (In fact, it can still be used after calling the Curl_global_cleanup function)
If this function is not called when the Curl_easy_init function is called, it will be done automatically by the Libcurl library.

Parameters: Flags

Curl_global_all//Initialize all possible calls.
CURL_GLOBAL_SSL//initialization supports Secure Sockets Layer.
CURL_GLOBAL_WIN32//Initialize Win32 socket font.
Curl_global_nothing//No additional initialization.


2 void Curl_global_cleanup (void);

Description: Used to clean up the work done by Curl_global_init at the end of Libcurl. A function similar to close.

3 char *curl_version ();

Description: Prints the version of the current Libcurl library.


4 CURL *curl_easy_init ();

Describe:
Curl_easy_init is used to initialize a pointer to curl (Some like a pointer that returns a file type). The corresponding at the end of the call to use the Curl_easy_cleanup function cleanup.
General Curl_easy_init means the beginning of a conversation. Its return value is typically used in functions of the easy series.

5 void Curl_easy_cleanup (Curl *handle);

Describe:
This call is used to end a session. With Curl_easy_init.

Parameters:
A pointer to the curl type.

6 Curlcode curl_easy_setopt (Curl *handle, curloption option, parameter);

Description: This function is most important. Almost all curl programs use it frequently.
It tells the Curl library how the program will behave. For example, to view the HTML code for a Web page.
(This function is somewhat like the IOCTL function)

Parameters:
1 pointer to curl type
2 options for various curloption types. (both are defined in the Curl.h library and can be viewed by man)
3 parameter This parameter can be either a pointer to a function, a pointer to an object, or a long variable. What it does with it depends on the second parameter.

Curloption This parameter takes a lot of value. The manual can be viewed in detail.

7 Curlcode curl_easy_perform (Curl *handle);

Description: This function is called after initializing a pointer of type curl and curl_easy_setopt completion. As the literal meaning says, perform is like a stage. Let's set the
option to work together.

Parameters:
A pointer to the curl type.

Let's look at a simple example:
To get the HTML code for a home page

#include <stdio.h>;

#include <curl/curl.h>;

#include <stdlib.h>;



int main (int argc, char *argv[])

{

Curl *curl;//pointer that defines the curl type

Curlcode res;//defining variables of type Curlcode



if (argc!=2)

{

printf ("Usage:file <url>;/n");

Exit (1);

}



Curl = Curl_easy_init ();//Initializes a pointer of type curl

if (curl!=null)

{

Set the Curl option. Where Curlopt_url is to have the user specify the URL. ARGV[1] stored in the command line in the URL

Curl_easy_setopt (Curl, Curlopt_url, argv[1]);

Call Curl_easy_perform to execute our settings. And do the related operations. It is only shown on the screen here.

res = curl_easy_perform (curl);

Clears the curl operation.

Curl_easy_cleanup (curl);

}

return 0;

}



Compilation: Gcc-o 001-wall 001.c-lcurl

Let's get the HTML code for the Www.chinaunix.net home page

./001 Www.chinaunix.net

Let's look at one more example:
We may not just show it when we are actually programming. Our goal is to get the HTML code to do the appropriate processing. such as checking keywords, discovering important information, etc.

Then we need to put the obtained HTML code into the corresponding file. Take a look at 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; Defining a file Type pointer



size_t write_data (void *ptr, size_t size, size_t nmemb, void *stream)//This function is designed to conform to Curlopt_writefunction, while 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 the subsequent action to the WRITE_DATA function processing

Curl_easy_perform (curl);

Curl_easy_cleanup (curl);

Exit (0);

}



Gcc-o 002-wall 002.c-lcurl
./002 Www.chinaunix.net
This example saves the HTML code in the Www.chinaunix.net file.

You can also get the header post form for the HTTP message, and so on. Here is not a detailed introduction. The concrete can be man curl_easy_setopt
(To use an important structure, httppost)



Here is an example of downloading a file from an FTP site.


#include <stdio.h>;

#include <curl/curl.h>;

#include <curl/types.h>;

#include <curl/easy.h>;



struct Ftpfile//defines a structure in order to be passed to the My_fwrite function. Available curl_easy_setopt curlopt_writedata option delivery

{

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 pointing to the struct ftpfile ftpfile.

if (out &&!out->;stream)

{

Out->;stream=fopen (Out->;filename, "WB"); Without this stream, create a name that is 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 ftpfile={argv[2],null}; Initialize a 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); A pointer to the fourth parameter of the related function passing a struct body

Curl_easy_setopt (Curl, Curlopt_verbose, TRUE); Curlopt_verbose This option is often used to display information returned on the screen for server-related operations



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 Fei.gif


I have an anonymous FTP URL to save the directory Fei.gif to local also known as Fei.gif


There are also curl_escape Curl_unescape functions used to convert Chinese characters into%XX this type. and convert back. If you want to download a file with Kanji. First call to convert the string.

Curl Library also has a lot of features. To be excavated by all.

Description: When GCC compiles, remember to add-lcurl

Curl/C + + API interface usage routines

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.