libcurl and reading in data

Started by
2 comments, last by JPulham 17 years, 11 months ago
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,"www.silvernova.co.uk/thegame/server.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "action=get");

res = curl_easy_perform(curl);

if(res == CURLE_OK)
{
    //read in data
    //HOW DO I DO THIS?
}
curl_easy_cleanup(curl);

thanks in advanced.
pushpork
Advertisement
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:

http://curl.haxx.se/libcurl/c/libcurl-tutorial.html
Thanks, this cleared alot up... I'll do that now.
pushpork
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 likemyxmlstream = 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>"
pushpork

This topic is closed to new replies.

Advertisement