C++ 用C+;中的URL插入数组中的字符串+;使用libcurl执行GET请求
我试图使用对应于每个名称的URL为五个不同的名称发出GET请求,但是Xcode一直在说C++ 用C+;中的URL插入数组中的字符串+;使用libcurl执行GET请求,c++,get,libcurl,string-interpolation,C++,Get,Libcurl,String Interpolation,我试图使用对应于每个名称的URL为五个不同的名称发出GET请求,但是Xcode一直在说curl\u easy\u setopt(curl,CURLOPT\u URL,namesURL)的行旁边告诉我它不能通过变量函数传递非平凡类型“std::_1::string”(又名“basic_string”)的对象;调用将在运行时中止 我的字符串插值是否存在导致此问题的问题?我尝试使用coutBotje回答了我在评论中的问题,指出通过传递namesURL.c_str()可以得到一个常规的const cha
curl\u easy\u setopt(curl,CURLOPT\u URL,namesURL)的行旁边告诉我代码>它不能通过变量函数传递非平凡类型“std::_1::string”(又名“basic_string”)的对象;调用将在运行时中止
我的字符串插值是否存在导致此问题的问题?我尝试使用coutBotje回答了我在评论中的问题,指出通过传递namesURL.c_str()
可以得到一个常规的const char*
而不是std::string
有关使用c_str()
的参考,请查看。尝试传递namesURL.c_str()
。这将为您提供一个常规的const char*
,而不是std::string
。这非常有效!我将把它作为最后的答案。curl\u easy\u setopt是否需要一个常量char
?或者URL中的字符串是一个变量函数,第二个参数决定第三个参数的解释方式。例如,CURLOPT\u TIMEOUT
需要以秒为单位的数字,而CURLOPT\u VERBOSE
需要0或1。
int main(){
string names[5] = {"Joe", "Suzy", "Larry", "Sally", "John"};
for(int i = 0; i < 5;i++){
string namesURL = "http://names.com/" + names[i];
cout << url << endl;
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, namesURL);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
}
}