Home php教程 php手册 开发的第一个PHP扩展

开发的第一个PHP扩展

Jun 06, 2016 pm 07:49 PM
php download develop Expand Source code First

下载php源码php-5.4.23.tar.gz,解压,进入/home/hubo/php-5.4.23/ext/扩展目录 wget http://cn2.php.net/get/php-5.4.23.tar.gz/from/this/mirror tar -xzvf php-5.4.23.tar.gz cd php-5.4.23/ext/ 在ext目录中新建config.m4文件 PHP_ARG_ENABLE(heiyoubo,

下载php源码php-5.4.23.tar.gz,解压,进入/home/hubo/php-5.4.23/ext/扩展目录

wget http://cn2.php.net/get/php-5.4.23.tar.gz/from/this/mirror

tar -xzvf php-5.4.23.tar.gz

cd php-5.4.23/ext/

在ext目录中新建config.m4文件

<span>PHP_ARG_ENABLE(heiyoubo,
    [Whether to enable the </span><span>"</span><span>heiyoubo</span><span>"</span><span> extension],
    [  enable</span>-heiyoubo        Enable <span>"</span><span>heiyoubo</span><span>"</span><span> extension support])

</span><span>if</span> test $PHP_HEIYOUBO != <span>"</span><span>no</span><span>"</span>; <span>then</span><span>
    PHP_SUBST(HEIYOUBO_SHARED_LIBADD)
    PHP_NEW_EXTENSION(heiyoubo, heiyoubo.c, $ext_shared)
</span><span>fi</span>
Copy after login

在ext目录中新建heiyoubo.c文件

<span>#ifdef HAVE_CONFIG_H
#include </span><span>"</span><span>config.h</span><span>"</span>
<span>#endif</span>

<span>//</span><span>加载php头文件</span>
#include <span>"</span><span>php.h</span><span>"</span>


<span>#define</span> phpext_heiyoubo_ptr &heiyoubo_module_entry<span>

ZEND_FUNCTION(heiyoubo_hello)
{
    php_printf(</span><span>"</span><span>Hello World Heiyoubo!\n</span><span>"</span><span>);
}

</span><span>static</span> zend_function_entry heiyoubo_functions[] =<span> { 
    ZEND_FE(heiyoubo_hello,        NULL)
    { NULL, NULL, NULL }
};


</span><span>//</span><span>module entry</span>
zend_module_entry heiyoubo_module_entry =<span> { 
</span><span>#if</span> ZEND_MODULE_API_NO >= 20010901<span>
     STANDARD_MODULE_HEADER,
</span><span>#endif</span>
    <span>"</span><span>heiyoubo</span><span>"</span>, <span>//</span><span>这个地方是扩展名称,往往我们会在这个地方使用一个宏。</span>
    heiyoubo_functions, <span>/*</span><span> Functions </span><span>*/</span><span>
    NULL, </span><span>/*</span><span> MINIT </span><span>*/</span><span>
    NULL, </span><span>/*</span><span> MSHUTDOWN </span><span>*/</span><span>
    NULL, </span><span>/*</span><span> RINIT </span><span>*/</span><span>
    NULL, </span><span>/*</span><span> RSHUTDOWN </span><span>*/</span><span>
    NULL, </span><span>/*</span><span> MINFO </span><span>*/</span>
<span>#if</span> ZEND_MODULE_API_NO >= 20010901
    <span>"</span><span>2.1</span><span>"</span>, <span>//</span><span>这个地方是我们扩展的版本</span>
<span>#endif</span><span>
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_HEIYOUBO
ZEND_GET_MODULE(heiyoubo)
</span><span>#endif</span>
Copy after login

 

运行phpize,准备 PHP 扩展库的编译环境

[hubo@test15169x ~/php-5.4.23/ext]$ /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525

configure的时候要开启heiyoubo扩展,并且指定php-config的目录,获取所安装的 PHP 配置的信息

[hubo@test15169x ~/php-5.4.23/ext]$ ./configure --enable-heiyoubo --with-php-config=/usr/local/php/bin/php-config

[hubo@test15169x ~/php-5.4.23/ext]$ make

[hubo@test15169x ~/php-5.4.23/ext]$ make test

heiyoubo.so扩展已经生成到module目录

[hubo@test15169x ~/php-5.4.23/ext]$ ll modules/*
-rw-rw-r-- 1 hubo hubo 799 01-09 16:53 modules/heiyoubo.la
-rwxrwxr-x 1 hubo hubo 26K 01-09 16:53 modules/heiyoubo.so

将heiyoubo.so文件拷贝到php的扩展目录

[hubo@test15169x ~/php-5.4.23/ext]$ php -ini | grep extension_dir
extension_dir => /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525 => /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525

[hubo@test15169x ~/php-5.4.23/ext]$ cp  modules/heiyoubo.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

将heiyoubo.so加到php的扩展文件/usr/local/etc/cgi/php.ini中  extension = "heiyoubo.so"

[hubo@test15169x ~/php-5.4.23/ext]$ php --ini
Configuration File (php.ini) Path: /usr/local/etc/cgi
Loaded Configuration File: /usr/local/etc/cgi/php.ini

[hubo@test15169x ~/php-5.4.23/ext]$ php -r 'var_dump(get_loaded_extensions());' | grep heiyoubo
string(8) "heiyoubo"

验证安装成功。执行C扩展中函数heiyoubo_hello();执行成功

[hubo@test15169x ~/php-5.4.23/ext]$ php -r 'heiyoubo_hello();'

Hello World Heiyoubo!

 

参考链接:

http://www.php.net/manual/zh/internals2.buildsys.configunix.php

https://github.com/walu/phpbook/blob/master/5.1.md           《Extending and Embedding PHP》中文版翻译      PHP扩展开发及内核应用

http://www.laruence.com/2009/04/28/719.html

http://www.php-internals.com/

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles