Call libcurl library to obtain face recognition results

1, Compilation and installation of openssl Library

  1. Create a new folder under the working directory. Download the openssl library compressed package from the official website and extract it to the folder

1|wget https://www.openssl.org/source/openssl-1.1.1.tar.gz
2|tar -vxf openssl-1.1.1.tar.gz

  1. Enter the extracted file directory, namely openssl-1.1 1a, execute the executable file config

1|./config

  1. compile

1|make


4. Download and install

1|sudo make install

2, libcurl library compilation and installation

  1. Download curl-7.71 1.tar. GZ compressed package
    Copy it to the new httpHandler file in the working directory through the shared folder, extract it and enter the extracted file, curl-7.71 one

1|tar -vxf curl-7.71.1.tar.gz
2|cd curl-7.71.1

  1. Run configure, -- prefix=$PWD/_install: Specifies that all files generated by compilation are placed in $PWD/_ In the install path folder ($means to get the current path), and – with SSL configures the openssl library downloaded and installed before into the libcurl library to enable it to access web pages that support https protocol.

1|./configure --prefix=$PWD/_install --with-ssl

  1. compile

1|make


3. Download and install

2|sudo make install


3, Programming to achieve face recognition and successfully obtain the recognition web page data results

  1. post request code
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>


#define true 1
#define false 0
typedef unsigned int bool;


char buffer[20480] = {'\0'};

size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
        strncpy(buffer, ptr, 20480);
        printf("=====================================get data=========================================\n");
        printf("%s\n", buffer);
}
char *getBase64FromPic(char *filePath)
{
        char *bufPic;
        char cmd[128] = {'\0'};
        int filelen;
        int fd;
        sprintf(cmd, "base64 %s > tmpfile", filePath);
        system(cmd);

        fd = open("./tmpfile", O_RDWR);
        filelen = lseek(fd, 0, SEEK_END);
        bufPic = (char *)malloc(filelen+2);
        lseek(fd, 0, SEEK_SET);
        memset(bufPic, '\0', filelen+2);
        read(fd, bufPic, filelen+2);
        close(fd);

        system("rm tmpfile -f");

        return bufPic;

}

bool postUrl()
{
        char *bufPic1;
        char *bufPic2;
        char *key = "8Pn4fG9m4bTrtDqf6CVbB8";
        char *secret = "1a0bf505815a4f8d8d7228e10c369f9e";
        int typeId = 21;
        char *format = "xml";
        char *postString;

        bufPic1 = getBase64FromPic("./jzt1.jpg");
        bufPic2 = getBase64FromPic("./jzt2.jpg");
        postString = (char *)malloc(strlen(key)+strlen(secret)+strlen(bufPic1)+strlen(bufPic2)+128);
        //Connect postString
        sprintf(postString, "&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s", bufPic1, bufPic2, key, secret, typeId, format);

        CURL *curl;
        CURLcode res;
        curl = curl_easy_init();
        if (curl)
        {
                curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // Specify cookie file
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);    // Specify post content
                curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do "); / / specify url
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData);
                res = curl_easy_perform(curl);
                printf("acquire success: %d\n", res);
                if (strstr(buffer, "yes") != NULL ){
                        printf("the same person\n");
                }
                else {
                        printf("different person\n");
                }

                curl_easy_cleanup(curl);
        }
        return true;
}
int main(void)
{
        postUrl();
}
~ 
  1. Operation results

    Web data:

    Summary: there are no errors when configuring and installing openssl library according to the above steps, but in facedemo 1 C is run when compiling, that is:
1|gcc faceDemo1.c -I curl-7.71.1/_install/include/ -L curl-7.71.1/_install/lib/ -lcurl

2|./a.out

The running result is not as shown in the figure above, only "acquire success: 1" is returned, ping www.baidu.com Com can also ping, and there is no problem checking the code, so I have been repeatedly installing the openssl library, which is of no help in the end. Then I restarted and set up a virtual machine. I also installed the openssl library and libcurl library according to the above operations. An error occurred when compiling and running a.out:
"./a.out:./a.out: error while loading shared libraries: libcurl.so.4: cannot open shared"
In this regard, I have referred to some solutions online (and stepped on some thunder). The methods are as follows:

1|take libcurl.so.4 Copy to root directory/usr/local/lib/(sudo cp....)

2|Command line input su Enter super user mode, and then vim /etc/ld.so.conf.d/usr-libs.conf´╝îEnter in this file/usr/local/lib Save and exit

3|ldconfig (to update/etc/ld.so.cache file)

4|exit (Exit superuser mode)

At this point, when compiling facedemo1 C can get json data when running a.out, and then I can get the correct results after compiling and running on my previous virtual machine (I'm really tired. If the first virtual machine compiles and runs a.out, if an error is reported, the problem can be solved long ago, which wastes me too much time configuring the openssl library. I'm really confused)

Keywords: SSL server https

Added by ridiculous on Fri, 31 Dec 2021 05:01:17 +0200