Sign in to follow this  

libcurl and reading in data

Recommended Posts

I've decided to get rid of HTTP-GET and use libcurl to send HTTP requests. It doesn't crash or cause bugs with my program. Now the problem is, how do I read in the web page? the php script I call will return (using echo();) an XML document. This contains the list of servers/games with a <server> tag. How can I take the stream of ANSI charecters and feed it into my XML reader. What function do I use in libcURL to get a char* string or something similar? Is there a differnt library needed?
CURL* curl;
CURLcode res;

curl = curl_easy_init();

curl_easy_setopt(curl, CURLOPT_URL,"");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "action=get");

res = curl_easy_perform(curl);

if(res == CURLE_OK)
    //read in data

thanks in advanced.

Share this post

Link to post
Share on other sites
The way to do it is to use the CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATA options to let libcurl write the received data to a temporary file (or just copy to some buffer).
Specifically, look at the ftpget.c example (in the 'examples' folder in libcurl source distribution) where these 2 options are demo'ed (it's an FTP example but these options work also for http protocols).

Also read an important detail about CURLOPT_WRITEFUNCTION if you're using libcurl.dll on Windows, here:

Share this post

Link to post
Share on other sites
Just double checking...
I have a function that follows the prototype:

size_t function( void *ptr, size_t size, size_t nmemb, void *stream);

//does something like
myxmlstream = ptr;

Is it safe to have this or will libcURL delete the stream pointer after it has been used? or will it stay untouched?
also, I will get an ANSI string like this wont I? (the \n ect... not the xml content) : "<xmlhead>\n<sometag>\n</xmlhead>"

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this