如何使用libcurl c ++从URL保存XML文件,以便稍后可以使用pugixml加载它。 我已经尝试过了,但是没有用
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
...
CURL *curl;
FILE *fp;
CURLcode result;
char *c_url = "http://some_url.xml";
char outfilename[FILENAME_MAX] = "sth.xml";
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, c_url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
result = curl_easy_perform(curl);
}
curl_global_init(CURL_GLOBAL_DEFAULT);
...
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(fp);
答案 0 :(得分:1)
通过如下方式将char*
设置为const
来修复错误之后:
char const* c_url = "http://some_url.xml";
您的代码对我来说很好。
但是,您并不总是要写入接收磁盘的数据。有时,您只想将其保留在内存中,以便即时使用它。
在这里,我编写了一个函数,可以将URL的目标下载到std::string
中,您可以使用它进行任何操作。我还使用智能指针使代码异常安全,并且通常更安全。
// write the data into a `std::string` rather than to a file.
std::size_t write_data(void* buf, std::size_t size, std::size_t nmemb,
void* userp)
{
if(auto sp = static_cast<std::string*>(userp))
{
sp->append(static_cast<char*>(buf), size * nmemb);
return size * nmemb;
}
return 0;
}
// To make the function thread safe you can use a smart pointer to
// hold your CURL session pointer.
// A deleter to use in the smart pointer for automatic cleanup
struct curl_dter{void operator()(CURL* curl) const
{ if(curl) curl_easy_cleanup(curl); }};
// A smart pointer to automatically clean up out CURL session
using curl_uptr = std::unique_ptr<CURL, curl_dter>;
// download the URL into a `std::string`.
std::string get_url(std::string const& url)
{
std::string data;
if(auto curl = curl_uptr(curl_easy_init()))
{
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
curl_easy_setopt(curl.get(), CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &data);
CURLcode ec;
if((ec = curl_easy_perform(curl.get())) != CURLE_OK)
throw std::runtime_error(curl_easy_strerror(ec));
}
return data;
}
int main()
{
curl_global_init(CURL_GLOBAL_DEFAULT);
auto xml = get_url("http://google.co.uk");
std::cout << xml << '\n';
curl_global_cleanup();
}
注意:我还添加了CURLOPT_FOLLOWLOCATION
选项,以防文档带有 redirect 。
答案 1 :(得分:0)
如果有人有兴趣..最后,我最终将使用以下代码:
const char* f = "new_file.xml";
if (curl){
const char* c_url = "some_url";
FILE* ofile = fopen(f, "wb");
if (!ofile) { fprintf(stderr, "Failed to open file: %s\n", strerror(errno)); }
if (ofile){
curl_easy_setopt(curl, CURLOPT_URL, c_url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, ofile);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_perform(curl);
fclose(ofile);
}
}
pugi::xml_document doc;
doc.load_file(f);
感谢所有帮助人员!