代码
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef unsigned int bool;
#define true 1
#define false 0
char buf[1024]={'\0'};
size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
strncpy(buf,ptr,1024);
// printf("================get data=============================\n");
// printf("%s\n",buf);
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char img1[12];
char img2[12];
char *key="4gk7iKdrBpLZsz3Wc5YskV";
char *secret="bcda1b84db53434eaa85d8f5f8078eeb";
int typeId=21;
char *format="xml";
char *postString;
system("base64 zerui1.jpg > tmpfile");
int fd=open("./tmpfile",O_RDWR);
int filelen=lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
char *bufpic1=(char*)malloc(filelen+2);
memset(bufpic1,0,filelen+2);
read(fd,bufpic1,filelen);
close(fd);
system("base64 zerui2.jpg > tmpfile");
fd=open("./tmpfile",O_RDWR);
filelen=lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
char *bufpic2=(char*)malloc(filelen+2);
memset(bufpic2,0,filelen+2);
read(fd,bufpic2,filelen);
close(fd);
int len= strlen(key)+strlen(secret)+strlen(bufpic1)+strlen(bufpic2)+124;
postString=(char*)malloc(len);
memset(postString,'\0',len);
sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",bufpic1,bufpic2,key,secret,21,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内容
curl_easy_setopt(curl, CURLOPT_URL,"https://netocr.com/api/faceliu.do"); // 指定url
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData); //将返回的http头输出到readData指向的文件
res = curl_easy_perform(curl);
printf("OK%d\n",res);
if(strstr(buf,"是")!=NULL){
printf("the same person\n");
}else{
printf("diff person\n");
}
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
// getUrl("/tmp/get.html");
postUrl();
}
比对结果: