mm4rs0
mm4rs0

Reputation: 3

How do I return cURL data

So I have this program that finds the external IP Address of my computer. I need to write this data as a variable, but I have no clue how to. Here's the source (I already know about the excessive headers, they were all being used at one time and I haven't cleaned it up yet.)

#include <stdio.h>
#include <C:\Documents and Settings\Jacob Grass\My Documents\C++\Server Software\curl\curl.h>
#include <C:\Documents and Settings\Jacob Grass\My Documents\C++\Server Software\curl\types.h>
#include <C:\Documents and Settings\Jacob Grass\My Documents\C++\Server Software\curl\easy.h>
#include <stdlib.h>
#include <unistd.h>
#include <windows.h>
#include <iostream>
#include <fstream>
//http://www.cplusplus.com/forum/windows/36638/
//http://bot.whatismyipaddress.com/
using namespace std;


int ipfetch (void)
{
  CURL *curl;
  CURLcode res;
  curl = curl_easy_init ();
  if (curl)
  {
    curl_easy_setopt (curl, CURLOPT_URL, "http://bot.whatismyipaddress.com/");
    res = curl_easy_perform (curl);
    curl_easy_cleanup (curl);

  }

  return 0;
}

int main (){
while(1){

ipfetch();

//Sleep in milliseconds
Sleep(300000);
cout<<"\n";

}



}

Any help would be appreciated.

Upvotes: 0

Views: 273

Answers (1)

ArtemGr
ArtemGr

Reputation: 12547

AFAIK, cURL is streaming: it won't hold the entire answer but would pass the chunks of the answer to the write function instead.

So, you need to setup a write function first, like this:

size_t curlWriteToString (void *buffer, size_t size, size_t nmemb, void *userp) {
  ((std::string*) userp)->append ((const char*) buffer, size * nmemb);
  return size * nmemb;};

...

std::string got;
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, curlWriteToString);
curl_easy_setopt (curl, CURLOPT_WRITEDATA, &got);

This will save the response into the got variable for you.

P.S. The code I pasted is from libglim/curl.hpp.

Upvotes: 1

Related Questions