cURL / Mailing Lists / curl-users / Single Mail

curl-users

Multiple URL

From: John MIddleton <chopsuey1973_at_hotmail.com>
Date: Wed, 07 Dec 2005 20:58:41 -0700

I have some code below where I am fetching a recordset from a MySQL database
and looping through the recordset and fetching the contents of each into
chunk.memory. The problem is the feeds are being concatenated in
chunk.memory. What is the correct way to free or reset chunk.memory so this
won't happen? I am assuming I should be able to reuse the curl_handle.

Thanks!

struct MemoryStruct {
   char *memory;
   size_t size;
};

int main(int argc, char **argv) {

   CURL *curl_handle;

   struct MemoryStruct chunk;

   chunk.memory = NULL;
   chunk.size = 0;

   //Setup CURL
   curl_global_init(CURL_GLOBAL_ALL);
   curl_handle = curl_easy_init();

   //I am fetching a recordset of feeds
   //Start while loop here
   while((row = mysql_fetch_row(res)) != NULL) { /* Get a row from the
results */
      printf("Feed: %s\n", row[1]);
      //Retrieve feed
      curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);
      curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
      curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
      curl_easy_setopt(curl_handle, CURLOPT_URL, row[1]);
      curl_easy_perform(curl_handle);

      printf("Contents: %s\n", chunk.memory);
      //Do some processing here

      //End while loop here
   }
   //Cleanup after CURL
   curl_easy_cleanup(curl_handle);

   //Free the memory we used to hold the feed
   if(chunk.memory) {
      free(chunk.memory);
   }

   return 0;
}
Received on 2005-12-08