在执行可执行文件时,由于翔云接口是https:开头的,需要libcurl库选择支持SSL。因此需要在配置的时候设置支持ssl
由/docs/INSTALL.md我们可以得知当用户已经有OpenSSL的时候不需要执行,如果没有安装OpenSSL,需要安装OpenSSL并使用命令./configure --with -ssl
进行配置,编译,安装
输入指令查看ubuntu是否安装openSSL:
openssl version -a
若没有安装OpenSSL则按照如下步骤:
rm -rf _install
上次编译出来的文件夹wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
tar xvf openssl-1.1.1a.tar.gz
./config
make
sudo make install
./configure --prefix=$PWD/_install --with-ssl
make
make install
#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 = NULL;
//根据翔云平台的接口要求 分开定义,然后字符串拼接
char* img1 = NULL; //图片base64流
char* img2 = NULL;
char* key = "xxxxxxxxxxx";
char* secret = "xxxxxxxxxxxxx";
int typeId = 21;
char* format = "xml";
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 demo.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib/ -lcurl
运行后的结果:
base64 photoname.jpg
char* getBase64FromFile(char* filePath)
{
char* base64Buf = NULL;
char cmd[256] = {'\0'};
sprintf(cmd,"base64 %s > tmpFile",filePath);//格式化输出,将图片的base64流导入到文件中
system(cmd);
int fd = open("./tmpFile",O_RDWR);
int fileLen = lseek(fd,0,SEEK_END); //计算文件大小
lseek(fd,0,SEEK_SET);
base64Buf = (char* )malloc(fileLen+8);//开辟空间
memset(base64Buf,'\0',fileLen+8);
read(fd,base64Buf,fileLen+8); //从文件中读取base64流到字符串
close(fd);
system("rm -f tmpFile");
return base64Buf;//指针变量(局部)随着子程序调用结束消失,但malloc开辟的空间地址还在,用指针变量指向该空间,然后返回
}
#include
#include
#include
#include
#include
#include
#include
#include
typedef unsigned int bool;
#define true 1
#define false 0
char ocrRetBuf[1024] = {'\0'};//全局变量,用来接收从OCR后台返回的数据
size_t readData(void *ptr, size_t size, size_t nmemb, void *stream)//回调函数,把从后台的数据拷贝给ocrRetBuf
{
strncpy(ocrRetBuf,ptr,1024);
}
char* getBase64FromFile(char* filePath)
{
char* base64Buf = NULL;
char cmd[256] = {'\0'};
sprintf(cmd,"base64 %s > tmpFile",filePath);
system(cmd);
int fd = open("./tmpFile",O_RDWR);
int fileLen = lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
base64Buf = (char* )malloc(fileLen+8);
memset(base64Buf,'\0',fileLen+8);
read(fd,base64Buf,fileLen+8);
close(fd);
system("rm -f tmpFile");
return base64Buf;
}
bool postUrl()
{
CURL *curl;
CURLcode res;
//根据翔云平台的接口要求 分开定义,然后字符串拼接
char* img1= getBase64FromFile("./zms.webp");//图片base64流
char* img2= getBase64FromFile("./zms.webp");
char* key = "xxxxxxxxxxx";
char* secret = "xxxxxxxxxxxxxx";
int typeId = 21;
char* format = "xml";
int len = strlen(key)+strlen(secret)+strlen(img1)+strlen(img2)+128;//分配空间不够会>导致栈溢出
char* postString = (char* )malloc(len);
memset(postString,'\0',len);//因为postString是一个指针,不能用sizeof来计算其指向的大小,用len接收字节数大小
//字符串拼接函数
sprintf(postString,"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_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);
if(strstr(ocrRetBuf,"否") != NULL){ //字符串检索 判断翔云后台返回的一大堆字符串中有没有“否”
printf("不是同一个人\n");
}
else{
printf("是同一个人\n");
}
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
return 0;
}
编译:
gcc demo.c -I ./curl-7.71.1/_install/include/ -L ./curl-7.71.1/_install/lib/ -lcurl