Thread: Reading XML returned from a POST

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    Reading XML returned from a POST

    Hello, im using a C program with libcurl to send a POST. The server reads my POST and return a XML file, that is printed in the terminal when i execute the program. How can i put the lines from this XML into variables in my C program? It doesnt seem to write this info anywhere?!

    Code:
    #include <stdio.h>
    #include <curl/curl.h>
      
    int main(void){
        CURL *curl;
        CURLcode res;
      
        curl = curl_easy_init();
        if(curl) {
           
            curl_easy_setopt(curl, CURLOPT_URL, "www.xxx.zzz.php");        
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "in_chkcacic=chkinfo");  
          
            res = curl_easy_perform(curl);
    
            curl_easy_cleanup(curl);
        }
        return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    man 3 libcurl will tell you how. You'll have to write a custom handler for the data, otherwise, the default handler will write the output to stdout.

    (FYI, I've never written a program with libcurl, and have been wanting to do this. So I took yours, figured out how to link it with Xcode, read the man pages, and Viola! It worked.)

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    I read the man page, but i cant make it work properly... Please correct me if im wrong. If i want to get the POST answer in a variable (char *) but the options CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATA are said to write in files...

    I also dont know which one i have to use, or if i have to use both. According to the manual:

    "If you're using libcurl as a win32 DLL, you MUST use the CURLOPT_WRITEFUNCTION if you set this option (CURLOPT_WRITEDATA) or you will experience crashes."

    This is the example i made using the options, based on some examples. Is it correct? How can i get the info i need?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <curl/curl.h>
    #include <curl/types.h>
    #include <curl/easy.h>
    
    
    struct MemoryStruct {
        char *memory;
        size_t size;
    };
    
    static void *myrealloc(void *ptr, size_t size) {
        /* There might be a realloc() out there that doesn't like reallocing
           NULL pointers, so we take care of it here */
        if(ptr)
    	return realloc(ptr, size);
        else
    	return malloc(size);
    }
    
    static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
        size_t realsize = size * nmemb;
        struct MemoryStruct *mem = (struct MemoryStruct *)data;
     
        mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
        if (mem->memory) {
    	memcpy(&(mem->memory[mem->size]), ptr, realsize);
    	mem->size += realsize;
    	mem->memory[mem->size] = 0;
        }
        return realsize;
    }
    
    
    int main(void){
        CURL *curl;
        CURLcode res;
      
        struct MemoryStruct chunk;
     
        chunk.memory=NULL; 
        chunk.size = 0;   
    
        curl = curl_easy_init();
        
        if(curl) {        
            curl_easy_setopt(curl, CURLOPT_URL, "www.xxx.zzz.php");
    
    	        
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "in_chkcacic=chkinfo");
    	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);  
    	       
            res = curl_easy_perform(curl);
              
            curl_easy_cleanup(curl);
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Dissecting an Excel XML spreadsheet
    By desmond5 in forum C++ Programming
    Replies: 1
    Last Post: 05-22-2008, 04:32 PM
  3. Using XML? My own binary system? A paint by number scheme?
    By Shamino in forum Game Programming
    Replies: 25
    Last Post: 03-10-2006, 01:08 AM
  4. 40 post drop
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-12-2001, 12:14 PM