Libcurl installation Method-Simple and practical (excerpt)

Source: Internet
Author: User

Http://curl.haxx.se/libcurl/c/example.html Official Website C example
Http://curl.haxx.se/download/curl-7.21.3.tar.gz

How to use
#tar ZXVF curl-7.21.3.tar.gz
#cd curl-7.21.3
#./configure
#make
#make Install
#gcc Sample.c-lcurl-o Sample
#./sample

The application of Curl Library in the program
a while ago wrote a crawl page code of the class, to a number of project requirements, the results found not stable, in the massive web page crawl, there are some exceptions to crawl failure. Although can meet the approximate requirements, but the function still can not let me 100% satisfaction, so in the webmaster's proposal, downloaded a Curl Universal library.
The first time to write such an article, there is biased, please understand, hehe.
recently put curl in their own projects, found that the effect is very ideal, especially in the massive data capture download, the failure rate is very low, the use of their own, here to give a point. In PHP, Curl uses more, but in C + +, the use of simple examples, and reference less, here I would like to summarize the use of curl in C + +. (Baidu Google's information is not very full, here to fill up a bit.)
Curl is a cross-platform library, http://curl.haxx.se/
If only command line tools are required, compile src under curl and if you need library references to directly compile the project under the master directory, the project will generate a src\ Dll-debug directory, copy out Libcurl.lib and Libcurl.dll. To an empty folder, and then put all the. h header files under the Include\curl folder in a folder.
line up, the material is ready, take the two folders, according to your own habits introduced into your project, you can.
under Linux, you can choose to create a build directory. The
then $./configure--prefix= The Bulid directory that you created, and then make, and finally install it on makes, and everything is in the build directory for you.
The following is the use of it, in fact, very simple, a few key APIs, not more than 4 commonly used. Very convenient, but some configuration parameters are relatively complex, it is strongly recommended that the http://curl.haxx.se/under the Help page, the use of all parameters and settings to say very clearly.
Well, oh, first of all, the simplest download page.
#include "./include/curl.h"
#include "./include/types.h"
#include "./include/easy.h"

These three header files must be referenced.
curl* M_pcurl;
Declares a Curl object. Here is a small suggestion, is recommended if you download a source site address, it is best to use a m_pcurl, the advantage is that when it and the site link, will maintain the link, if you download the page is originated from this site, it will maximize the savings of your system resources. If every time you download a Web page is a new M_pcurl object, you will see the netstat-an inside countless time_wiat linked objects, consumption of resources is not really necessary.
M_pcurl = Curl_easy_init ();
Initializes a Curl object, which generates a curl pointer back. If the return is null, a link failure is established. In fact, the likelihood of failure here is very small, because it only does an initialization action. Initialize a soket and some buffer memory buff, generally if return is null, then see if your network card has a problem.
Since then is the most critical to get the page Information section, first say get, say post. (some HTTPS-based encrypted transmissions are not discussed here first)
If it is a normal get method:
BOOL Cdownicon:ownloadicon (const char* psoftid, const char* PURL)
{
Curlcode Curlres;
Chtmldatabuff M_htmlbuff;
struct Curl_slist *chunk = NULL;

if (m_pcurl! = NULL)
{
Chunk = Curl_slist_append (Chunk, "accept-encoding:gzip, deflate");
Chunk = Curl_slist_append (chunk, "user-agent:mozilla/4.0" (compatible; MSIE 8.0; Windows NT 5.1; CIBA) ");
Chunk = Curl_slist_append (chunk, "connection:keep-alive");

Download file
Curl_easy_setopt (M_pcurl, Curlopt_httpheader, Chunk);
Curl_easy_setopt (M_pcurl, Curlopt_timeout, 120);
Curl_easy_setopt (M_pcurl, Curlopt_url, PURL);
Curl_easy_setopt (M_pcurl, curlopt_writefunction, url_iconwrite);
Curl_easy_setopt (M_pcurl, Curlopt_writedata, &m_htmlbuff);

Curlres = Curl_easy_perform (M_pcurl);
if (curlres = = CURLE_OK)
{
Web page Download success, the downloaded page exists in my Chtmldatabuff object inside, in fact this is very simple, if not related to gzip and other compressed format download, you can use a string to replace my Chtmldatabuff
}

Curl_slist_free_all (chunk);
Return Ftpupload (Psoftid);
}
Else
{
return false;
}
}
As mentioned above, I explain a little bit what is doing, hehe.
The first curl_slist_append () function is useful, because if you do not write anything, curl transmits a simple protocol like "Get/Your Web page Accept: */*", and in some servers with more stringent authentication, such HTTP link protocol words will be discarded. That is, nothing to return you, not to be depressed, curl designers have already given you to think well, Curl_slist_append () This API can let you disguise as a standard Web browser request, admittedly, you also see, I append some HTTP options, These options will be appended to your HTTP request. This will allow you to successfully pass the rigorously verified server and let it return the correct data to you. Of course, the premise here is that you have to have some understanding of the HTTP 1.1 protocol.
Curl_easy_setopt (M_pcurl, Curlopt_httpheader, Chunk);
This API is the setting option for curl, which allows you to set almost hundreds of curl control options. Here are only a few of the most commonly used ones, and if you want to go further, please visit http://curl.haxx.se/here for a complete explanation. (Of course, your e-text to be enough clearance)
This means that the string that is set by the chunk is appended to the HTTP request message header.
Curl_easy_setopt (M_pcurl, Curlopt_timeout, 120);
This means that if the server does not return in 120 seconds, curl triggers a timeout error when the timeout period is set. The recommended setting here is to prevent your code from waiting indefinitely for the return of the server at certain special moments.
Curl_easy_setopt (M_pcurl, Curlopt_url, PURL);
Purl is your web address, such as "Http://www.google.com/", of course, not only can the Web page, can also be js,jpg and so on, such as "http://www.163.com/1.jpg" is also possible. Actually say Curlopt_url is a bit narrow, it can download any URL link can point to something. Including images, SWF, and so on.
Curl_easy_setopt (M_pcurl, curlopt_writefunction, url_iconwrite);
Curl_easy_setopt (M_pcurl, Curlopt_writedata, &m_htmlbuff);
Next is the two sentences, Url_iconwrite is actually a callback interface, you may ask, why do you want to do this? I just want the web itself something good, hehe, in fact, in order to serve another purpose, that is the download progress, you see a lot of browsers have a download progress bar in the go, hehe, right, through this callback function, you can design your download progress bar, it will give you to download the progress and data block at this moment, In particular, when the chunk mode transfer return protocol is supported, it triggers several callbacks based on each chunk.
The Curlopt_writedata option is to specify an object that you will use to stitch the received pieces of data into a complete set when the callback function.
int url_iconwrite (void *buffer, size_t size, size_t nmemb, void *stream)
{
size_t Stdatalen = size * NMEMB;
char* pData = new Char[stdatalen];
if (NULL = = PData)
{
return 0; Returns an error that triggers a receive failure and stops receiving.
}
chtmldatabuff* Phtmldatabuff = (chtmldatabuff*) stream;

memcpy (Szdata, (char*) buffer, Stdatalen);

Phtmldatabuff->adddata (PData, (int) stdatalen); Glue the packets together, and you can use your own method here.

Delete[] PData;

return Stdatalen;
}
This is the binding callback function of the wording, of course, url_iconwrite I up, you can use your favorite name.
Phtmldatabuff->adddata (szdata, (int) stdatalen); This sentence is in order to assemble the packets I received. You can write something like this yourself according to your logic, or simply use a string, + +, or whatever.
Buffer is a pointer to the current data block, size * NMEMB is the length of the current data, and the stream is actually the object you are binding with Curlopt_writedata.
All right, keep talking.
Curlres = Curl_easy_perform (M_pcurl);
This is the beginning of the execution of your URL download activity, he returned a Curlres object, in fact, the feeling is an int, if successful, will return a CURLE_OK tag, conversely, will give you a number, you can find the corresponding explanation in curl.h.
You must set Curl_slist_free_all (chunk) When you have completed a crawl, and you do not have to do this unless your chunk is the same as it was the next time you use it. But eventually you have to Curl_slist_free_all (chunk), otherwise there will be a memory leak.
Finally, when you finish your Web crawl, be sure not to forget Curl_easy_cleanup (M_pcurl), release your object, otherwise, your memory will be leaked.
OK, the above is a standard URL file download or Web crawl code. Of course it's get mode.
The next post, in fact, is also very simple, as long as a little modification of a little place can be achieved post.
Curl_easy_setopt (PCurl, curlopt_post, 8080);
Curl_easy_setopt (PCurl, Curlopt_postfields, szdata);
The Curlopt_post parameter is set to your Psot port address, such as my example is 8080.
The Curlopt_postfields parameter is attached to the data content of your specific post, and you can make this part yourself.
Of course, the General Post protocol is a good time to attach a chunk.
M_chunk = Curl_slist_append (M_chunk, "content-length:xxxxx"); XXXXX is the length of your post data. Otherwise, some servers may think your request is illegal.
Well, say another interesting application.
FTP is now used in many places as a server.
So how do you make an FTP request with curl? It's also possible here. (Download is easy, say a more complex upload it.) )
Take code as an example:
BOOL Cdownicon::ftpupload (const char* Psoftid)
{
Curlcode Curlres;
Char szserverpath[http_iconmax_1024] = {'} '};

sprintf (Szserverpath, "ftp://127.0.0.1/%s", Psoftid);

file* fp = NULL;
fp = fopen (M_szfilename, "RB");
if (NULL = = fp)
{
printf ("[Main]fopen (%s) fail!. \ n "," a.jpg ");
return false;
}

Fseek (FP, 0l, seek_end);
int nfilesize = (int) ftell (FP);

if (nfilesize <= 0)
{
printf ("[Creadfile::readfile]ftell error (%d)!\n", nfilesize);
}

Fseek (FP, 0l, Seek_set);

if (m_pftpcurl! = NULL)
{
Curl_easy_setopt (M_pcurl, Curlopt_verbose, TRUE); This parameter can display the FTP instruction in the FTP procedure, if you want to see it.
Curl_easy_setopt (M_pftpcurl, Curlopt_userpwd, "Freeeyes:freeeyes");
Curl_easy_setopt (M_pftpcurl, Curlopt_url, Szserverpath);
Curl_easy_setopt (M_pftpcurl, Curlopt_put, 1);
Curl_easy_setopt (M_pftpcurl, Curlopt_infile, FP);
Curl_easy_setopt (M_pftpcurl, Curlopt_infilesize, (curl_off_t) (size_t) nfilesize);
Curl_easy_setopt (M_pftpcurl, Curlopt_ftp_create_missing_dirs, 1);

Curlres = Curl_easy_perform (M_pftpcurl);
if (curlres = = CURLE_OK)
{
Fclose (FP);
return true;
}
Else
{
Fclose (FP);
printf ("[Cdownicon::ftpupload] (%s) Upload fail.\n", Psoftid);
return false;
}
}
Else
{
return false;
}
}
See, in fact, and HTTP almost, but to pay attention to a few parameters.
Curl_easy_setopt (M_pftpcurl, Curlopt_userpwd, "Freeeyes:freeeyes");
This is the setting of your FTP user name and address. Of course, your FTP does not have a password to ignore this option.
Curl_easy_setopt (M_pftpcurl, Curlopt_infile, FP);
This FP is the file* pointer you want to upload.
Curl_easy_setopt (M_pftpcurl, Curlopt_infilesize, (curl_off_t) (size_t) nfilesize);
This specifies the size of the file you are uploading.
Curl_easy_setopt (M_pftpcurl, Curlopt_url, Szserverpath);
This specifies the name of your uploaded file, such as "/img/001/1001/1001.jpg"
Curl_easy_setopt (M_pftpcurl, Curlopt_ftp_create_missing_dirs, 1);
The most interesting is this option, I like it, this option means that if you do not have such a path on the remote FTP, curl will help you to build well. Save me a lot of mkdir trouble, even a multi-level directory It can help you build, reduce a lot of code.
Oh, see, in fact, curl used in the FTP is also quite simple. And it's cross-platform.
These are some of my initial understandings of curl, which I'll share with you here. I hope that the experience will help you when you use it that day.
Of course, this is only the most basic use. A.

Libcurl installation Method-Simple and practical (excerpt)

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.