以下部分内容参考博文:
https://blog.csdn.net/weixin_50438937/article/details/113630094?spm=1001.2014.3001.5502
//Test2.c
#include
#include
typedef unsigned int bool;//数据类型别名用typedef 有分号
#define true 1 //宏定义(替换)用define 无冒号
#define false 0
bool getUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL) // 返回结果用文件存储
return false;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: Agent-007");
curl = curl_easy_init(); // 初始化
if (curl)
{
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
curl_easy_setopt(curl, CURLOPT_URL,"http://www.baidu.com");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将返回的http头输出到fp指向的文件
curl_easy_setopt(curl, CURLOPT_HEADERDATA, fp); //将返回的html主体数据输出到fp指向的文件
res = curl_easy_perform(curl); // 执行
if (res != 0) {
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
}
bool postUrl(char *filename)
{
CURL *curl;
CURLcode res;
FILE *fp;
if ((fp = fopen(filename, "w")) == NULL)
return false;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86"); // 指定post内容
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");
curl_easy_setopt(curl, CURLOPT_URL, " http://mail.sina.com.cn/cgi-bin/login.cgi "); // 指定url
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
fclose(fp);
return true;
}
int main(void)
{
getUrl("/tmp/get.html");
postUrl("/tmp/post.html");
}
#include
#include
#include
#include
typedef unsigned int bool;//数据类型别名用typedef
#define true 1 //宏定义用define 就是宏替换
#define false 0
//回调函数,读取从OCR后台返回的数据 前面打通百度的例子是将内容读取到本地文件fd里面
size_t readData(void *ptr, size_t size, size_t nmemb, void *stream)
{
char buf[1024] = {'\0'};
strncpy(buf,ptr,1024);
printf("===========get data ===========\n");
printf("%s\n",buf);
}
bool postUrl() //根据文档,接口调用方法为post请求
{
CURL *curl;
CURLcode res;
char* postString;//野指针,要开辟空间
//根据翔云平台的接口要求 分开定义,然后字符串拼接
char* img1 = NULL; //图片base64流 还未生成
char* img2 = NULL;
char* key = "xxx";
char* secret = "xxx";
int typeId = 21;
char* format = "xml";
//游标卡尺刻度 //2个image+typeId+format
postString = (char* )malloc(strlen(key)+strlen(secret)+2048);
//字符串拼接函数 仅测试,两张图片为空
sprintf(postString,"img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s","","",key,secret,typeId,format);
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie缓存文件
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);//指定post传输内容,get请求将URL和postString一次性发送
curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");// 指定url
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,readData); //回调函数读取返回值
res = curl_easy_perform(curl);
printf("OK:%d\n",res);
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
}
输入指令:
gcc Test2.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib/ -lcurl
Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。
Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。采用Base64编码具有不可读性,需要解码后才能阅读。
Linux中查看base64
base64 meinv.png
#include
#include
#include
#include
#include
#include
#include
#include
#define true 1
#define false 0
typedef unsigned int bool;
char* faceRes = NULL;
char* getBase64(char* photoPath){// 获取照片(jpg格式)的Base64的值
char cmd[256] = {'\0'};
sprintf(cmd, "base64 %s > photoBase64File", photoPath);
system(cmd);// 通过执行"base64"这个指令即可得到照片的Base64值,在这里将得到的Base64值存放在photoBase64File文件中
int fd,size;
fd = open("./photoBase64File", O_RDWR);
size = lseek(fd, 0, SEEK_END) + 1;
lseek(fd, 0, SEEK_SET);
char* res = (char*)malloc(size);
memset(res, '\0', size);
read(fd, res, size);// 从photoBase64File文件中读取照片的Base64值
close(fd);
system("rm photoBase64File");
return res;
}
size_t handle(void *ptr, size_t size, size_t nmemb, void *stream){
//拷贝返回来的结果字段
int ssize = strlen(ptr) + 1;
faceRes = (char*)malloc(ssize);
memset(faceRes, '\0', ssize);
strncpy(faceRes, ptr, ssize);
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char* message = NULL;
// 调用getBase64()自定的函数获取存放在当前文件夹下的两个进行识别的图片的Base64值
char* img1 = getBase64("./photo1.jpg");
char* img2 = getBase64("./photo2.jpg");
// key值和secret值是在翔云官网->个人中心的OCR Key和OCR secret两个的值
char* key = "用自己的";
char* secret = "用自己的";
int typeId = 21;
char* format = "xml";
int size = strlen(img1)+strlen(img2)+strlen(key)+strlen(secret)+strlen(format)+3;
message = (char*)malloc(size);
memset(message, '\0', size);
sprintf(message, "&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
img1, img2, key, secret, typeId, format);
curl = curl_easy_init(); // 初始化
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");// 指定url
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);// 指定post内容
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle);// 当拿到结果后,指定调用handle()该自定的函数进行处理
res = curl_easy_perform(curl);// 进行访问官网、进行人脸识别的操作
curl_easy_cleanup(curl);// 执行完后,对curl_easy_init()进行清理
}
if(res != 23){
printf("post failed!\n");
return false;
}else{
printf("post successful!\n");
}
printf("%s\n",faceRes);// 打印出人脸识别后返回来的结果字段
if(strstr(faceRes, "是") != NULL){// 如果返回来的结果字段中有“是”这个字眼,代表是同一个人
printf("This is the same person!\n");
}else{
printf("Tow different persons!\n");
}
free(faceRes);
free(img1);
free(img2);
}
int main(void)
{
postUrl();
return 0;
}
gcc demo1.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib/ -lcurl