---
title: "php开发扩展程序"
author: "ciaos"
date: "2015-10-19T20:12:28Z"
tags: []
categories: ["编程语言"]
slug: "php"
draft: false
lastmod: "2015-10-19T20:12:27Z"
id: 2250130
url: "https://ciaos.iteye.com/blog/2250130"
---

下载php-src 5.6.13版本（试过5.3版本编译不过）

```
[root]# ./ext_skel --extname=pkey
Creating directory pkey
Creating basic files: config.m4 config.w32 .gitignore pkey.c php_pkey.h CREDITS EXPERIMENTAL tests/001.phpt pkey.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/pkey/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-pkey
5.  $ make
6.  $ ./sapi/cli/php -f ext/pkey/pkey.php
7.  $ vi ext/pkey/pkey.c
8.  $ make
```

编辑config.m4时，去掉下面几行注释

```
PHP_ARG_ENABLE(pkey, whether to enable pkey support,
Make sure that the comment is aligned:
[  --enable-pkey           Enable pkey support])

PHP_SUBST(PKEY_SHARED_LIBADD)

PHP_NEW_EXTENSION(pkey, pkey.c, $ext_shared)
```

编辑pkey.c时定义自己的函数和实现

```
const zend_function_entry pkey_functions[] = {
        //PHP_FE(confirm_pkey_compiled, NULL)           /* For testing, remove later. */
        PHP_FE(getpkey, NULL)   
        PHP_FE_END      /* Must be the last line in pkey_functions[] */
};

PHP_FUNCTION(getpkey)
{
        char *arg = NULL;
        int arg_len, len;
        char *strg;

        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
                return;
        }

        len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "pkey", arg);
        RETURN_STRINGL(strg, len, 0);
}
```

 php.ini中加入extension引入扩展使用即可。