3 Replies - 309 Views - Last Post: 24 August 2019 - 11:10 PM Rate Topic: -----

#1 AlphaGiraffe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-August 19

Using cURLpp and libcURL

Posted 24 August 2019 - 04:24 PM

View PostCodebug, on 14 November 2011 - 09:07 PM, said:

I have been fighting this for hours to no avail. I have no idea how to set the paths, which paths I even need to set, which files I need to link to, etc. It's so confusing coming from Java and not having to worry about this. A few questions, and then I think I am going to try another avenue.

1) Do I need to be using cURLpp AND libcURL?
2) Which parts of cURLpp and libcURL do I need to be linking to and setting paths to?
3) Is there an alternative to cURLpp that is easier to get set up and running that avoids setting all of the paths and whatnot?


I know it's been 8 years but did you have any luck figuring this out? I've having a similar issue. Not sure why it's so hard to do a quick http request in c++.

Is This A Good Question/Topic? 0
  • +

Replies To: Using cURLpp and libcURL

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7020
  • View blog
  • Posts: 23,840
  • Joined: 05-May 12

Re: Using cURLpp and libcURL

Posted 24 August 2019 - 04:39 PM

Please do no revive old threads, especially since the OP that you are asking hasn't logged on since 2013. Start your own thread and ask your own questions. I'm going to split off your question for now...
Was This Post Helpful? 0
  • +
  • -

#3 AlphaGiraffe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-August 19

Re: Using cURLpp and libcURL

Posted 24 August 2019 - 10:23 PM

View PostSkydiver, on 24 August 2019 - 04:39 PM, said:

Please do no revive old threads, especially since the OP that you are asking hasn't logged on since 2013. Start your own thread and ask your own questions. I'm going to split off your question for now...


Sorry about that. I found an answer tho. Not sure if i should post it on this thread or the old thread.
For people coming across this looking for a way to get curl to work in c++, ill share my solution.

I'm on ubuntu, so i installed curl with
apt-get install libcurl4-gnutls-dev
.

I was also using eclipse c++, i linked the curl library to my code by going to Project->Properties->C/C++ Build->Settings->Cross G++ Linker->Libraries and adding "curl" to the Libraries list
Include it into your code
#include <curl/curl.h>


And finally:
CURL *curl;
	CURLcode res;
	curl = curl_easy_init();

	if (curl) {
		curl_easy_setopt(curl, CURLOPT_URL, url_of_the_site);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, DataRetrieval::WriteCallback);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &db_data);
		res = curl_easy_perform(curl);
		if (res != CURLE_OK) {
			cout << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
		}

		curl_easy_cleanup(curl);
	}


2 notes on this: the CURLOPT_WRITEFUNCTION option and CURLOPT_WRITEDATA option.

the value for CURLOPT_WRITEDATA is the address of a string you'd like the response of the request to be saved in. For me it's string db_data;

as for CURLOPT_WRITEFUNCTION, it's a weird function that i honestly don't understand but copied and pasted anyways. Would not recommend copying and pasting code you don't understand. but here it is:
size_t DataRetrieval::WriteCallback(char *contents, size_t size, size_t nmemb, void* userp) {
	((std::string*) userp)->append((char*) contents, size * nmemb);
	return size * nmemb;
}

Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2369
  • View blog
  • Posts: 4,496
  • Joined: 30-May 10

Re: Using cURLpp and libcURL

Posted 24 August 2019 - 11:10 PM

The first thing you absolutely must do is make DataRetrieval::WriteCallback a static function.

C knows NOTHING about 'this' pointers, and thus cannot call C++ member functions directly.

But it can call static member functions, because these do not have an implied 'this'.

So something like this.
#include <iostream>

using namespace std;

class DataRetrieval {
  std::string db_data;
  size_t WriteData(char *contents, size_t size, size_t nmemb) {
    db_data.append(contents,size*nmemb);
    return size * nmemb;
  }
public:
  static size_t WriteCallback(char *contents, size_t size, size_t nmemb, void* userp) {
    // make a 'this' out of the userp pointer.
    DataRetrieval *me = reinterpret_cast<DataRetrieval*>(userp);
    return me->WriteData(contents,size,nmemb);
  }
};


int main()
{
  DataRetrieval myDr;
  
  // Point to the static member function
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &DataRetrieval::WriteCallback);
  // Point to some instance of the class
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &myDr);

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1