c++ - 我可以在 C++ 上使用 curl
问题描述
我在互联网上搜索在 C++ 上使用 curl 但它变得更加困惑
我使用 curl 像这样从我的电报机器人发送消息
curl -F text="<My Text>" https://api.telegram.org/bot<Bot Token>/sendMessage?chat_id=<Chat id>
有什么办法可以在 C++ 中做同样的事情
解决方案
是的,你可以运行
curl -F text="<My Text>" https://api.telegram.org/bot<Bot Token>/sendMessage?chat_id=<Chat id> --libcurl example.c
这将在 example.c 中生成一个完全执行您的请求的 C 代码。您可以在 C++ 代码中使用此示例。
/********* Sample code generated by the curl command line tool **********
* All curl_easy_setopt() options are documented at:
* https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
************************************************************************/
#include <curl/curl.h>
int main(int argc, char *argv[])
{
CURLcode ret;
CURL *hnd;
struct curl_httppost *post1;
struct curl_httppost *postend;
post1 = NULL;
postend = NULL;
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "text",
CURLFORM_COPYCONTENTS, "<My Text>",
CURLFORM_END);
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.telegram.org/bot<Bot Token>/sendMessage?chat_id=<Chat id>");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_HTTPPOST, post1);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.55.1");
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
/* Here is a list of options the curl code used that cannot get generated
as source easily. You may select to either not use them or implement
them yourself.
CURLOPT_WRITEDATA set to a objectpointer
CURLOPT_INTERLEAVEDATA set to a objectpointer
CURLOPT_WRITEFUNCTION set to a functionpointer
CURLOPT_READDATA set to a objectpointer
CURLOPT_READFUNCTION set to a functionpointer
CURLOPT_SEEKDATA set to a objectpointer
CURLOPT_SEEKFUNCTION set to a functionpointer
CURLOPT_ERRORBUFFER set to a objectpointer
CURLOPT_STDERR set to a objectpointer
CURLOPT_HEADERFUNCTION set to a functionpointer
CURLOPT_HEADERDATA set to a objectpointer
*/
ret = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
hnd = NULL;
curl_formfree(post1);
post1 = NULL;
return (int)ret;
}
/**** End of sample code ****/
推荐阅读
- php - 在 html/php 中的单独表行中显示目录中的文件名
- python - 如何在图像上添加遮罩以模糊它们的交点?
- mysql - 为什么 MySQL 从 5.7 升级到 8.0 后出现重复输入错误?
- apache-nifi - 是否可以进行 Nifi 应用程序备份和恢复?
- asp.net-core - Entity Framework Core 未加载 1 到 0 外键实体
- java - 服务层和spring事务中的验证
- java - Spring JpaRepository ENUM 不匹配
- akka-stream - Akka Streams:具有自定义逻辑的扇出运算符
- request - 创建peyment(条纹)时出错
- javascript - 如果传递的项目存在于 arrayOfArrays 内的所有数组中,则返回 true