C++ libcurl won';t加载URL的内容
我正在尝试加载此URL的内容以便发送SMSC++ libcurl won';t加载URL的内容,c++,curl,libcurl,C++,Curl,Libcurl,我正在尝试加载此URL的内容以便发送SMS https://app2.simpletexting.com/v1/send?token=[api密钥]&电话=[电话号码]&消息=天气%20警报 使用实现libcurl的这段代码: std::string sendSMS(std::string smsMessage, std::string usrID) { std::string simplePath = "debugOld/libDoc.txt"; std::string
https://app2.simpletexting.com/v1/send?token=[api密钥]&电话=[电话号码]&消息=天气%20警报代码>
使用实现libcurl的这段代码:
std::string sendSMS(std::string smsMessage, std::string usrID) {
std::string simplePath = "debugOld/libDoc.txt";
std::string preSmsURL = "https://app2.simpletexting.com/v1/send?token=";
std::cout << "\n" << getFile(simplePath) << "\n";
std::string fullSmsURL = preSmsURL + getFile(simplePath) + "&phone=" + usrID + "&message=" + smsMessage;
std::cout << fullSmsURL;
//Outputs URL contents into a file
CURL *curl;
FILE *fd;
CURLcode res;
char newFile[FILENAME_MAX] = "debugOld/noSuccess.md";
curl = curl_easy_init();
if (curl) {
fd = fopen(newFile, "wb");
curl_easy_setopt(curl, CURLOPT_URL, fullSmsURL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fd);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fd);
}
}
std::string sendSMS(std::string smsMessage,std::string usrID){
std::string simplePath=“debugOld/libDoc.txt”;
std::string preSmsURL=”https://app2.simpletexting.com/v1/send?token=";
std::cout这行是错误的:
curl_easy_setopt(curl, CURLOPT_URL, fullSmsURL);
CURLOPT\u URL
需要指向以null结尾的C字符串的char*
指针,而不是std::string
对象。您需要使用此指针:
curl_easy_setopt(curl, CURLOPT_URL, fullSmsURL.c_str());
此外,您根本没有对getFile()
、fopen()
、或curl\u easy\u perform()
的返回值执行任何错误检查。因此,您的代码可能在这些位置中的任何一个位置失败,并且您永远不会知道它。是否检查了任何恶意字符(例如换行符)由getFile
返回?可能会显示该函数的实现。我已经用getFile函数编辑了文章。在getFile()中,我看不到会导致它的任何内容,虽然我现在不相信自己。谢谢!请尝试输出它返回的字符串的长度,并检查它是否与您期望的字符数匹配。只需查看您对CURL的调用(我没有使用该调用的经验),我忍不住注意到它在C风格的接口中接受任意参数,因此如果您将fullSmsUrl
传递给它(它是std::string
),事情可能会出大问题。std::string
不会隐式地将自身转换为const char*
。试试这个:curl\u easy\u setopt(curl,CURLOPT_URL,fullSmsURL.c_str());
这似乎也是正确的。读取文件时会得到32个字符,这相当于字符串长度。为了防止我的计数错误,字符串是(类似于)1JB1D0C0632 D40E6HC78B06B5C5F9AFI对于卷曲来说是非常新的,一般来说是新的C++。我正在查找一些关于如何检查这些函数的资源。您有什么资源推荐吗?
curl_easy_setopt(curl, CURLOPT_URL, fullSmsURL.c_str());