首先请前往Server酱的页面按要求注册。
然后编译以下源码运行即可(注意修改POSTURL
的SCKEY
),源码的post请求部分参考了这篇文章
另外,编译时请使用
gcc source.c -l curl -o output
否则会报错。
以下是源码部分。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#define POSTURL "https://sc.ftqq.com/[SCKEY].send"
#define POSTFIELDS s_post
#define FILENAME "ftqq_curl.log"
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp){
FILE *fptr = (FILE*)userp;
fwrite(buffer, size, nmemb, fptr);
fclose(fptr);
}
int main(int argc, char* argv[]){
char s_desp[65537];
char s_post[256 + 65536 + 11 + 1];
if(argc != 3){
printf("\nInvalid Arguments!\nusage:<command dir> <text> <desp or desp dir>");
return 1;
}
FILE *fp = NULL;
if((fp = fopen(argv[2],"r")) == NULL)strcpy(s_desp, argv[2]);
else fread(s_desp, sizeof(s_desp) - 1, 1, fp);
fclose(fp);
if(strlen(argv[1]) <= 256)sprintf(s_post, "text=%s&desp=%s", argv[1], s_desp);
else{
printf("\nText is too long!\n");
return 2;
}
CURL *curl;
CURLcode res;
FILE *fptr;
struct curl_slist *http_header = NULL;
if ((fptr = fopen(FILENAME, "w")) == NULL){
fprintf(stderr, "fopen file error: %s\n", FILENAME);
exit(1);
}
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, POSTFIELDS); //设置post属性,使用&来将表单属性连接在一起
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //回调函数,可有可无
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr); //回调函数写入数据指针
curl_easy_setopt(curl, CURLOPT_POST, 1); //设置libcurl发送的协议
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0); //设置详细模式
curl_easy_setopt(curl, CURLOPT_HEADER, 1); //设置http数据头
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //设置返回的数据量
//curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/Users/zhu/CProjects/curlposttest.cookie"); //设置cookie,不是必须
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}