Install C extension in PHP under Ubuntu

1. Server environment

- Ubuntu 16.04.2 LTS x64
- PHP 7.0.21

2. Server-side software installation

Install apache2

    apt-get install apache2

install php

    apt-get install php7.0
    apt-get install php7.0-dev
    apt-get install libapache2-mod-php7.0
   
    // Ubuntu 14.04, php5.6.32
    apt-get install python-software-properties
    add-apt-repository ppa:ondrej/php
    apt-get update
    apt-get install -y php5.6
    apt- get install php5.6-dev

3. Add to the system public library

Execute the following command to add the library file to the system public library.

    cp libmyclass1.so /usr/local/lib/ #Copy to the system public library
    cp libmyclass2.so /usr/local/lib/
    ldconfig #Load library

Fourth , install the php extension

Download the php source code and use the ext_skel tool in the source package to create the extension.

Download php source code

Download the source code version corresponding to the current version of php. Use the following command to check the current php version

    php -v

My version is 7.0.21, go to the php official website to download the corresponding tar.gz file

    wget http://php.net/distributions/php-7.0.21.tar.gz

decompress and Go to the ext directory

    tar -zxvf php-7.0.21.tar.gz
    cd php-7.0.21/ext

to create the mythcrypt extension./ext_skel

    --extname=myclass1 Update the config.m4 file and remove the dnl     cd

on lines 16 and 18

mythcrypt
    vim config.m4
   
    16: PHP_ARG_ENABLE(myclass1, whether to enable mythcrypt support,
    17: dnl Make sure that the comment is aligned:
    18: [ --enable-myclass1 Enable myclass1 support])

Update myclass1.c

    vim myclass1.c

    // Add function declaration
    const zend_function_entry myclass1_functions[] = {
        PHP_FE(confirm_myclass1_compiled, NULL) /* For testing, remove later. */
        PHP_FE(my_function, NULL)
        PHP_FE(my_request, NULL)
        PHP_FE_END / * Must be the last line in mythcrypt_functions[] */
    };
   
    // Add the following code below the PHP_FUNCTION(confirm_myclass1_compiled) function definition. Different versions add different syntaxes. The following are examples of versions 5.3.10, 5.6.31, and 7.0.21.
   
    // 5.3.10
    PHP_FUNCTION(my_function)
    {
        char *ret;
   
        ret=my_function();
        RETURN_STRING(ret,0);
    }
    PHP_FUNCTION(my_request)
    {
        char *dataid;
        char *ret;
   
        .....;
    }
   
    // 5.6.31
    PHP_FUNCTION(my_function)
    {
        char dataout[1024]={0};
   
        my_function(dataout);
        RETURN_STRING(dataout, 1);
    }
    PHP_FUNCTION(my_request)
    {
        int datalen;
        char *dataid;
        char dataout[1024]={0};
   
        .....;
    }
   
    // 7.0.21
    PHP_FUNCTION(my_function)
    {
        char dataout[1024]={0};
   
        my_function(dataout);
        RETURN_STRING(dataout);
    }
    PHP_FUNCTION(my_request)
    {
        char *dataid;
        char dataout[1024]={0};
   
        .....;
    }

In versions 5.6.31 and 7.0.21, the return strings of the functions my_function and my_request The maximum length is 1024.

Use the phpize command to generate the configure file

    phpize
    ./configure
    make LDFLAGS=-lmyclass1 # Load libmycalss1.so and make
    make test # Test
    make install # Install the library into the extension folder of php

Update php configuration file and add the mythcrypt library

    vim / etc/php/7.0/apache2/php.ini

    extension=myclass1.so

    # Restart apache
    /etc/init.d/apache2 restart


5. Write a test script The

test script is as follows:

    <?php
    echo my_function();
    echo my_request(1, '{"user_id":"test","crypto_service_id":1,"signature":"testasdfawef","data_in":{"sn":"A1000012312234234","hwid":"12312123234234234"}}');
   


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326057437&siteId=291194637