搜索
首页php教程php手册用PHP扩展做一个HelloWorld!
用PHP扩展做一个HelloWorld!Jul 11, 2016 pm 08:00 PM
php扩展代码开源编程编程语言软件开发

PHP 尽管提供了大量有用的函数,但是在特殊情况下还可能需要进行扩展编程,比如大量的 PECL(PHP Extension Community Library)就是以扩展的形式提供的(动态链接库dll文件),它们比 PEAR 的运行效率要高很多。
    PHP 扩展是用 C 或 C++ 编写的,需要编译成动态连接库 dll 文件后在 PHP 环境下注册后才能使用。
    编写 PHP 扩展的软件要求:
      VC++6.0 或 VC++.NET 环境。
      PHP 的源代码,需要编译。
    如果不愿意编译 PHP 的源代码,可以再下载 PHP 的已经编译成功的二进制代码(就是我们部署 PHP 运行环境的那些文件包)。注意分别下载的源文件包和已编译包,它们的版本必须一致。

    过程:

    1,安装 VC++6.0,并选择把其可执行文件路径加入环境变量中,使在命令行环境任意路径下可以运行编译器。
    2,安装 PHP 运行环境,并与 IIS 正确集成在一起。假设使用的 PHP 版本为 5.2.5,下载 php-5.2.5-Win32.zip 二进制包和 php-5.2.5.tar.gz 源代码包。安装环境为 C:\php-5.2.5-Win32。分别把源代码包和二进制包解压到该文件夹下。从 php.ini-recommended 拷贝生成一个 php.ini 文件。
    3,建立 C:\php-5.2.5-Win32\Release_TS 文件夹,拷贝 C:\php-5.2.5-Win32\dev\php5ts.lib 文件到这里。
    4,进入 C:\php-5.2.5-Win32\ext 文件夹,运行命令:
      C:\php-5.2.5-Win32\ext>..\php.exe ext_skel_win32.php --extname=myphpext
      Creating directory myphpext
      Creating basic files: config.m4 config.w32 .cvsignore myphpext.c php_myphpext.h
      CREDITS EXPERIMENTAL tests/001.phpt myphpext.php [done].

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

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

      Repeat steps 3-6 until you are satisfied with ext/myphpext/config.m4 and
      step 6 confirms that your module is compiled into PHP. Then, start writing
      code and repeat the last two steps as often as necessary.

    结果在 ext 下生成一个文件夹 myphpext,包含一个 PHP 扩展应用编程框架。myphpext 可以任意取名,将来生成的 dll 文件格式为 php_[extname].dll,我们生成的就是 php_myphpext.dll。运行结果的提示信息 1.2...8 主要是对 Linux/Unix 环境而言的,我们不必理会。其实 config.m4 文件在 Windows 下也可能需要修改,但是对于我们简单的框架暂时还用不着。

    文件夹 myphpext 包含若干个文件,其中:

    myphpext.dsp 是工程文件,后边还要用;
    myphpext.php 扩展测试文件;
    php_myphpext.h 扩展函数定义头文件
    myphpext.c 扩展函数具体实现

    以上 2 个重要的文件内容:

    php_myphpext.h 文件:

/*
      +----------------------------------------------------------------------+
      | PHP Version 5                                                        |
      +----------------------------------------------------------------------+
      | Copyright (c) 1997-2007 The PHP Group                                |
      +----------------------------------------------------------------------+
      | This source file is subject to version 3.01 of the PHP license,      |
      | that is bundled with this package in the file LICENSE, and is        |
      | available through the world-wide-web at the following url:           |
      | 
http://www.php.net/license/3_01.txt                                  |
      | If you did not receive a copy of the PHP license and are unable to   |
      | obtain it through the world-wide-web, please send a note to          |
      | license@php.net so we can mail you a copy immediately.               |
      +----------------------------------------------------------------------+
      | Author:                                                              |
      +----------------------------------------------------------------------+
    
*/


    
/* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */

    #ifndef PHP_MYPHPEXT_H
    
#define PHP_MYPHPEXT_H

    
extern zend_module_entry myphpext_module_entry;
    
#define phpext_myphpext_ptr &myphpext_module_entry

    #ifdef PHP_WIN32
    
#define PHP_MYPHPEXT_API __declspec(dllexport)
    
#else
    
#define PHP_MYPHPEXT_API
    
#endif

    #ifdef ZTS
    #include 
"TSRM.h"
    
#endif

    PHP_MINIT_FUNCTION(myphpext);
    PHP_MSHUTDOWN_FUNCTION(myphpext);
    PHP_RINIT_FUNCTION(myphpext);
    PHP_RSHUTDOWN_FUNCTION(myphpext);
    PHP_MINFO_FUNCTION(myphpext);

    PHP_FUNCTION(confirm_myphpext_compiled); 
/* For testing, remove later. */
    PHP_FUNCTION(HelloPHP);

    
/*
       Declare any global variables you may need between the BEGIN
     and END macros here:

    ZEND_BEGIN_MODULE_GLOBALS(myphpext)
     long  global_value;
     char *global_string;
    ZEND_END_MODULE_GLOBALS(myphpext)
    
*/


    
/* In every utility function you add that needs to use variables
       in php_myphpext_globals, call TSRMLS_FETCH(); after declaring other
       variables used by that function, or better yet, pass in TSRMLS_CC
       after the last function argument and declare your utility function
       with TSRMLS_DC after the last declared argument.  Always refer to
       the globals in your function as MYPHPEXT_G(variable).  You are
       encouraged to rename these macros something shorter, see
       examples in any other php module directory.
    
*/


    #ifdef ZTS
    
#define MYPHPEXT_G(v) TSRMG(myphpext_globals_id, zend_myphpext_globals *, v)
    
#else
    
#define MYPHPEXT_G(v) (myphpext_globals.v)
    
#endif

    
#endif /* PHP_MYPHPEXT_H */

    
/*
     * Local variables:
     * tab-width: 4
     * c-basic-offset: 4
     * End:
     * vim600: noet sw=4 ts=4 fdm=marker
     * vim      
*/


  

  myphpext.c 文件:

    

/*
      +----------------------------------------------------------------------+
      | PHP Version 5                                                        |
      +----------------------------------------------------------------------+
      | Copyright (c) 1997-2007 The PHP Group                                |
      +----------------------------------------------------------------------+
      | This source file is subject to version 3.01 of the PHP license,      |
      | that is bundled with this package in the file LICENSE, and is        |
      | available through the world-wide-web at the following url:           |
      | 
http://www.php.net/license/3_01.txt                                  |
      | If you did not receive a copy of the PHP license and are unable to   |
      | obtain it through the world-wide-web, please send a note to          |
      | license@php.net so we can mail you a copy immediately.               |
      +----------------------------------------------------------------------+
      | Author:                                                              |
      +----------------------------------------------------------------------+
    
*/


    
/* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */

    #ifdef HAVE_CONFIG_H
    #include 
"config.h"
    
#endif

    #include 
"php.h"
    #include 
"php_ini.h"
    #include 
"ext/standard/info.h"
    #include 
"php_myphpext.h"

    
/* If you declare any globals in php_myphpext.h uncomment this:
    ZEND_DECLARE_MODULE_GLOBALS(myphpext)
    
*/


    
/* True global resources - no need for thread safety here */
    
static int le_myphpext;

    
/* {{{ myphpext_functions[]
     *
     * Every user visible function must have an entry in myphpext_functions[].
     
*/

    zend_function_entry myphpext_functions[] 
= {
     PHP_FE(confirm_myphpext_compiled, NULL)  
/* For testing, remove later. */
     PHP_FE(HelloPHP, NULL)
     
{NULL, NULL, NULL} /* Must be the last line in myphpext_functions[] */
    }
;
    
/* }}} */

    
/* {{{ myphpext_module_entry
     
*/

    zend_module_entry myphpext_module_entry 
= {
    
#if ZEND_MODULE_API_NO >= 20010901
     STANDARD_MODULE_HEADER,
    
#endif
     
"myphpext",
     myphpext_functions,
     PHP_MINIT(myphpext),
     PHP_MSHUTDOWN(myphpext),
     PHP_RINIT(myphpext),  
/* Replace with NULL if there's nothing to do at request start */
     PHP_RSHUTDOWN(myphpext), 
/* Replace with NULL if there's nothing to do at request end */
     PHP_MINFO(myphpext),
    
#if ZEND_MODULE_API_NO >= 20010901
     
"0.1"/* Replace with version number for your extension */
    
#endif
     STANDARD_MODULE_PROPERTIES
    }
;
    
/* }}} */

    #ifdef COMPILE_DL_MYPHPEXT
    ZEND_GET_MODULE(myphpext)
    
#endif

    
/* {{{ PHP_INI
     
*/

    
/* Remove comments and fill if you need to have entries in php.ini
    PHP_INI_BEGIN()
        STD_PHP_INI_ENTRY("myphpext.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_myphpext_globals, myphpext_globals)
        STD_PHP_INI_ENTRY("myphpext.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_myphpext_globals, myphpext_globals)
    PHP_INI_END()
    
*/

    
/* }}} */

    
/* {{{ php_myphpext_init_globals
     
*/

    
/* Uncomment this function if you have INI entries
    static void php_myphpext_init_globals(zend_myphpext_globals *myphpext_globals)
    {
     myphpext_globals->global_value = 0;
     myphpext_globals->global_string = NULL;
    }
    
*/

    
/* }}} */

    
/* {{{ PHP_MINIT_FUNCTION
     
*/

    PHP_MINIT_FUNCTION(myphpext)
    
{
     
/* If you have INI entries, uncomment these lines
     REGISTER_INI_ENTRIES();
     
*/

     
return SUCCESS;
    }

    
/* }}} */

    
/* {{{ PHP_MSHUTDOWN_FUNCTION
     
*/

    PHP_MSHUTDOWN_FUNCTION(myphpext)
    
{
     

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何查看php用了哪些扩展如何查看php用了哪些扩展Aug 01, 2023 pm 04:13 PM

查看phpinfo()函数输出、使用命令行工具和检查PHP配置文件均可以查看php用了哪些扩展。1、查看phpinfo()函数输出,创建一个简单的PHP脚本,将这个脚本保存为phpinfo.php,并将其上传到您的Web服务器,在浏览器中访问此文件,使用浏览器的搜索功能,在页面中查找关键字"extension"或"extension_loaded",以找到有关扩展的信息即可。

如何使用php扩展PDO连接Oracle数据库如何使用php扩展PDO连接Oracle数据库Jul 29, 2023 pm 07:21 PM

如何使用PHP扩展PDO连接Oracle数据库导语:PHP是一种非常流行的服务器端编程语言,而Oracle是一款常用的关系型数据库管理系统。本文将介绍如何使用PHP扩展PDO(PHPDataObjects)来连接Oracle数据库。一、安装PDO_OCI扩展要连接Oracle数据库,首先需要安装PDO_OCI扩展。以下是安装PDO_OCI扩展的步骤:确保

如何使用PHP扩展SuiteCRM的报告生成功能如何使用PHP扩展SuiteCRM的报告生成功能Jul 19, 2023 am 10:27 AM

如何使用PHP扩展SuiteCRM的报告生成功能SuiteCRM是一款功能强大的开源CRM系统,它提供了丰富的功能来帮助企业管理客户关系。其中一个重要的功能就是报告生成,使用报告可以帮助企业更好地了解业务情况,并作出正确的决策。本文将介绍如何使用PHP扩展SuiteCRM的报告生成功能,并提供相关的代码示例。在开始之前,需要确保已经安装好了SuiteCRM,

PHP入门指南:PHP扩展安装PHP入门指南:PHP扩展安装May 20, 2023 am 08:49 AM

在使用PHP进行开发时,我们可能需要使用一些PHP扩展。这些扩展可以为我们提供更多的功能和工具,使我们的开发工作更加高效和便捷。但在使用这些扩展之前,我们需要先进行安装。本篇文章将为您介绍PHP扩展的安装方法。一、什么是PHP扩展?PHP扩展是指为PHP编程语言提供额外功能和服务的组件。这些组件可以通过PHP的扩展机制进行安装和使用。PHP扩展可以帮助我们处

宝塔面板的PHP扩展和PHP版本管理宝塔面板的PHP扩展和PHP版本管理Jun 21, 2023 am 08:49 AM

宝塔面板是一款开源的服务器管理面板,在为网站运营者提供便捷的网站管理、数据库管理、SSL证书管理等服务的同时,还提供了强大的PHP扩展和PHP版本管理功能,让服务器管理变得更加简单和高效。一、PHP扩展PHP扩展是一种用来增强PHP功能的模块,通过安装PHP扩展可以实现更多的功能和服务,比如:加速器:加速器可以显著地提高PHP性能,通过缓存PHP脚本,减轻服

php如何使用PHP的geoip扩展?php如何使用PHP的geoip扩展?Jun 01, 2023 am 09:13 AM

PHP是一种流行的服务器端脚本语言,它可以处理网页上的动态内容。PHP的geoip扩展可以让你在PHP中获取有关用户位置的信息。在本文中,我们将介绍如何使用PHP的geoip扩展。什么是PHP的GeoIP扩展?PHP的geoip扩展是一个免费的、开源的扩展,它允许你获取有关IP地址和位置信息的数据。该扩展可以与GeoIP数据库一起使用,这是一个由MaxMin

教程:使用极光推送及其PHP扩展在应用中添加消息推送功能教程:使用极光推送及其PHP扩展在应用中添加消息推送功能Jul 26, 2023 am 08:07 AM

教程:使用极光推送及其PHP扩展在应用中添加消息推送功能引言:在如今的移动应用开发中,消息推送功能已经成为了各类应用必不可少的一部分。而极光推送则是这方面最常用、最受开发者欢迎的解决方案之一。本教程将介绍如何使用极光推送及其PHP扩展来在应用中添加消息推送功能,并提供相应的代码示例供参考。一、极光推送简介极光推送是一款基于云服务的、跨平台的消息推送解决方案。

完全指南:如何使用php扩展SimpleXML读取和处理XML数据完全指南:如何使用php扩展SimpleXML读取和处理XML数据Jul 28, 2023 pm 02:46 PM

完全指南:如何使用PHP扩展SimpleXML读取和处理XML数据简介:在现代的Web开发中,处理和操作XML数据是一项非常常见的任务。PHP作为一种强大的服务器端脚本语言,提供了多种扩展和功能,用于处理和操作XML数据。其中,SimpleXML扩展是一种特别有用的工具,可以简化XML数据的读取和处理过程。本文将为您提供一个完整的指南,介绍如何使用PHP扩展

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器