LibCurl CURLOPT_URL不接受字符串吗? C ++ [英] LibCurl CURLOPT_URL not accepting string? C++
问题描述
所以基本上我想做的是使用libcurl来获取稍微不同的网址,例如:
So basically what I want to do is use libcurl to fetch slightly different urls, e.g.:
http://foo.com/foo.asp?name=*NAMEHERE*
我想做的是遍历一个名称向量并获得每个名称,例如:
What I would like to do is iterate through a vector of names and get each one, e.g.:
http://foo.com/foo.asp?name=James
然后
http://foo.com/foo.asp?name=Andrew
以此类推.
但是,当我尝试这样做时:
However, when I try doing this:
int foo (){
CURL *curl;
CURLcode success;
char errbuf[CURL_ERROR_SIZE];
int m_timeout = 15;
if ((curl = curl_easy_init()) == NULL) {
perror("curl_easy_init");
return 1;
}
std::vector<std::string> names;
names.push_back("James");
names.push_back("Andrew");
for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i){
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, long(m_timeout));
curl_easy_setopt(curl, CURLOPT_URL, "http://foo.com/foo.asp?name=" + *i);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1L);
}
if ((success = curl_easy_perform(curl)) != 0) {
fprintf(stderr, "%s: %s\n", "curl_easy_perform", errbuf);
return 1;
}
curl_easy_cleanup(curl);
return 0;
}
它给我一个错误:
不能通过可变函数传递非平凡类型'std :: __ 1 :: basic_string< char>'的对象;调用将在运行时中止
Cannot pass object of non-trivial type 'std::__1::basic_string<char>' through variadic function; call will abort at runtime
在这一行:
curl_easy_setopt(curl, CURLOPT_URL, "http://foo.com/foo.asp?name=" + *i);
因为+ *i
.
我想做些什么吗?有解决办法吗?
Is what I want to do possible? Is there a solution?
感谢您的回答,但是由于某些原因,当我运行此命令时,它只会获取带有向量中最后一个字符串的网站,而忽略其他网站.就我而言,它跳过James
并直接进入Andrew
.为什么会发生这种情况?
Thanks for the answer, but for some reason, when I run this it only gets the website with the last string in the vector, and it ignores the other ones. In my case, it skips James
and goes straight to Andrew
. Why does that happen?
推荐答案
传递给curl_easy_setopt
的CURLOPT_URL
的参数必须是char *
而不是std::string
.您可以通过调用c_str
成员函数从std::string
获取const char *
:
The parameter passed to curl_easy_setopt
for CURLOPT_URL
needs to be a char *
instead of a std::string
. You can get a const char *
from a std::string
by calling its c_str
member function:
std::string url = "http://foo.com/foo.asp?name=" + *i;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
对于以后到这里来的任何人,请查看 curl手册如果您还没有的话.还有一本在线书.
For anyone who ends up here in the future, take a look at the curl manual if you haven't already. There's also an online book.
curl_easy_setopt 的文档说,您需要阅读有关特定选项的信息知道要使用哪种参数类型.
The documentation for curl_easy_setopt says you need to read about a specific option to know what parameter type to use.
所有选项均设置有选项,后跟参数.该参数可以是long,函数指针,对象指针或curl_off_t,取决于特定选项的期望.仔细阅读本手册,因为错误的输入值可能会导致libcurl表现不佳!
All options are set with an option followed by a parameter. That parameter can be a long, a function pointer, an object pointer or a curl_off_t, depending on what the specific option expects. Read this manual carefully as bad input values may cause libcurl to behave badly!
CURLOPT_URL 的文档确切说明了参数类型是什么
The documentation for CURLOPT_URL says exactly what the parameter type needs to be.
传入指向要使用的URL的指针. 该参数应为char *到零终止字符串,该字符串必须以以下格式进行网址编码:
Pass in a pointer to the URL to work with. The parameter should be a char * to a zero terminated string which must be URL-encoded in the following format:
方案://主机:端口/路径
scheme://host:port/path
这篇关于LibCurl CURLOPT_URL不接受字符串吗? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!