zoukankan      html  css  js  c++  java
  • 使用C语言开发PHP扩展(转)

    本篇文章给大家带来的内容是介绍如何使用C语言给PHP写扩展,,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

    1、在php源码路径的ext文件夹下,新建一个extend_test.def文件,编辑文件内容为:

    1

    string my_test_function(string str,int n)

    2、在当前目录执行

    1

    ./ext_skel --extname=extend_test --proto=extend_test.def

    其中,extname是扩展的名,proto是刚创建的文件,也可以用全路径

    3、在当前目录下会生成extend_test文件夹,编辑extend_test文件夹下的config.m4

    去掉一下三行的dnl的注释

    1

    2

    3

    PHP_ARG_ENABLE(extend_test, whether to enable extend_test support,

    Make sure that the comment is aligned:

    [  --enable-extend_test           Enable extend_test support])

    4、编辑extend_test文件夹下的extend_test.c文件

    找到以下方法并修改

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    PHP_FUNCTION(my_test_function)

    {

        char *str = NULL;

        int argc = ZEND_NUM_ARGS();

        size_t str_len;

        zend_long n;

        char *result;

        char *ptr;

        zend_long result_length;

        if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)

            return;

        result_length = str_len * n;

        result = (char *) emalloc(result_length + 1);

        ptr = result;

        while (n--) {

            memcpy(ptr, str, str_len);

            ptr += str_len;

        }

        *ptr = '';

        RETURN_STRINGL(result, result_length);

    }

    5、生成扩展:

    在extend_test文件夹下,运行/home/php/bin/phpize(实际为phpize所在路径)

    然后运行./configure --with-php-config=/home/php/bin/php-config(实际为php-config所在路径)

    6、编译安装

    1

    2

    make            

    make install

    7、执行完后会显示扩展安装到了哪个路径下

    然后修改php.ini增加扩展信息

    1

    extension=extend_test.so

    8、测试扩展是否可用

    新建test.php文件并将内容编辑为

    1

    2

    <?php

    echo my_test_function('a',5);

    保存后用php运行,显示出aaaaa表示扩展成功安装

  • 相关阅读:
    List of 3rd Party .NET UI & Reporting Components
    cannot open window service on computer '.' in window application
    CentOS7重新生成 /boot/grub2/grub.cfg
    vmware esxi 查看网卡、Raid卡驱动
    libcmt.lib和msvcrt.lib冲突,原因和解决方法
    C/C++注释规范
    lsof命令查看端口关联的文件
    linux下查看磁盘分区的文件系统格式
    编译linux kernel及制作initrd ( by quqi99 )
    三种虚拟化技术比较
  • 原文地址:https://www.cnblogs.com/shixiuxian/p/10619548.html
Copyright © 2011-2022 走看看