Reading XML returned from a POST

This is a discussion on Reading XML returned from a POST within the C Programming forums, part of the General Programming Boards category; Hello, im using a C program with libcurl to send a POST. The server reads my POST and return a ...

  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
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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, 05: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, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21