问题
why isn't the cookies storing? the local website I made has a setcookie("test",time()); in PHP and it works in the browser normally, but in cURL it just won't work, I don't even know what to try anymore, this is the simplest I got by trimming the code, and still don't save the cookies in a file. it doesn't even create the file to save it (btw the windows has the permissions to add any file in the root since I'm moving and creating files all the time there), and Yes the site example set cookies. what is wrong with it ?
The content output is correct and it should work. but it just doesn't work. :/
thanks in advance.
#include <curl/curl.h>
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main () {
CURL *curl2;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
string url2 = "http://localhost/test.php";
url2 = "http://www.facebook.com";
curl2 = curl_easy_init();
if(curl2) {
string html2 = "";
struct curl_slist *chunk = NULL;
curl_easy_setopt(curl2, CURLOPT_URL, url2.c_str());
curl_easy_setopt(curl2, CURLOPT_COOKIEFILE, "C:\\teste.txt");
curl_easy_setopt(curl2, CURLOPT_COOKIEJAR, "C:\\teste.txt");
curl_easy_setopt(curl2, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl2, CURLOPT_WRITEDATA, &html2);
res = curl_easy_perform(curl2);
std::cout << "RESULT: " << html2;
}
_getch();
return 0;
}
回答1:
Remember to call curl_easy_cleanup(curl2)
来源:https://stackoverflow.com/questions/9884048/cookies-not-being-saved-in-curl-c-such-a-simple-and-clean-code-example-wont