Sign in to follow this  
JPulham

libcurl and reading in data

Recommended Posts

JPulham    120
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.

Share this post


Link to post
Share on other sites
basement    229
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

Share this post


Link to post
Share on other sites
JPulham    120
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