我正在尝试更改URL的搜索词,而不是在C++的cURL中预定义一个搜索词。谁能帮我解决这个问题?
我在下面提供了一些关于我尝试的方法的代码,但无济于事:
#include <stdio.h>
#include <iostream>
#include <curl/curl.h>
using namespace::std;
int main(void)
{
CURL *curl;
CURLcode res;
string searchTerm;
cout<<"Enter ticker name: "<<endl;
cin>>searchTerm;
cout<<"https://ichart.finance.yahoo.com/table.csv?s="<<searchTerm<<endl;
string whatURL = "https://ichart.finance.yahoo.com/table.csv?s="+searchTerm;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, whatURL); //the error I get is in this line
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* 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);
}
//int argc, const char * argv[]
return 0;
}
我得到的实际错误是:无法通过可变参数函数传递非平凡类型"字符串"(又名"basic_string,分配器>")的对象;调用将在运行时中止
注意:我在网上找到了一种使用 PHP 工作的方法,但是我不知道如何将 PHP 实现到我的C++文件中(作为一种可能的解决方法)。
提前感谢,
你必须记住,libcurl 是一个 C API,只有 C 函数。幸运的是,您可以直接从C++使用它们,但不能使用 C 中不可用的任何内容(例如对象实例)调用它们。
这里你需要传递一个字符串指针,你可以使用 std::string::c_str
函数来获取它:
curl_easy_setopt(curl, CURLOPT_URL, whatURL.c_str());