Home Backend Development PHP Tutorial windows上制作PHP扩展

windows上制作PHP扩展

Jun 13, 2016 pm 01:24 PM
nbsp php visual windows

windows下制作PHP扩展

windows下制作PHP扩展
2011年01月26日
  转自:http://demon.tw/software/compile-php-on-windows.ht ml
  编译PHP扩展必需的一些头文件需要从php源码中获取,其中有一些配置性的头文件,需要做到下面第8步之后,也就是说php服务器程序可以使用别人已经编译好的安装包。 最近在学习编写PHP拓展,懒得装Linux,就研究了一下在Windows下编译PHP的方法,总算搭建好了在Windows下开发PHP拓展的环境。
  1、在C盘新建一个文件夹,C:\PHPDEV
  2、从官网下载最新版(我这里用的是5.2.14)的PHP源码,将下tar.gz或tar.bz2压缩包保存到C:\PHPDEV,解压到当前文件夹
  3、下载php win32 build extras并保存到C:\PHPDEV,解压到当前文件夹
  4、获取编译好的bison.exe和flex.exe,保存到桌面或者其他C:\PHPDEV以外的地方。分别打开压缩包并进入bin文件夹,将bison.exe和flex.exe复制到C:\Windows文件夹
  5、假定你已经安装了Visual Studio 2008(我个人不推荐用Visual Studio 2008,推荐使用Platform SDK Febrary 2003,官方的编译版本就是用这个PSDK编译的,但是这里用Visual Studio 2008做说明),开始菜单->Microsoft Visual Studio 2008->Visual Studio Tools->Visual Studio 2008 Command Prompt,打开Visual Studio 2008命令提示行
  6、用cd命令将目录切换到C:\PHPDEV\php-5.2.14
  7、输入buildconf.bat,回车。这个批处理的作用是搜索所有的.w32文件并为你创建configure.js
  8、输入下面的命令
  cscript /nologo configure.js  without-xml  without-wddx  without-simplexml  without-dom  without-libxml  disable-zlib - without-sqlite  disable-odbc  disable-cgi  enable-cli  enable-debug  without-iconv  disable-ipv6
  为什么要disable和without那么多功能呢?因为这些功能需要的库文件并没有包含在PHP的源码包中(不然会很大),这些额外的库文件需要你自己去下载(要找全不是那么容易的)。如果你不禁用这些功能,会出现编译错误。
  9、输入nmake,回车
  10、第9步中可能会出现文件的编码错误,找到出现错误的文件,用EditPlus选择Western European (Windows)编码打开后另存为utf-8编码,重写nmake即可
  11、编译好以后切换到C:\PHPDEV\php-5.2.6\Debug_TS,测试一下
  php -r "echo 'hello,world';" 二、windows下开发PHP扩展
  转自:http://blog.csdn.net/linvo/archive/2009/04/17/4086 909.aspx
  第一步:准备
  1、php源码包和windows下的二进制包,以及安装Visual C++,并把Microsoft Visual Studio\Common\MSDev98\Bin的绝对路径添加到windows环境变量
  2、解压源码包到d:\php_src
  3、进入d:\php_src\ext目录,复制skeleton文件夹,并重命名为要开发扩展的名字,本例为"linvo"
  4、把二进制包中dev目录下的php5ts.lib文件,拷入新建的linvo目录
  5、编辑linvo目录中的php_skeleton.h、skeleton.c、skeleton.dsp这三个文件,替换内容中所有extname为linvo,EXTNAME为LINVO。(严格区分大小写)
  第二步:编码
  6、编辑php_skeleton.h文件(头文件)
  在PHP_FUNCTION(confirm_linvo_compiled);下面编写
  PHP_FUNCTION(hello);
  声明一个hello函数
  7、编辑skeleton.c文件(主文件)
  在PHP_FE(confirm_linvo_compiled, NULL) 下面编写
  PHP_FE(hello, NULL)
  这是函数入口,下面该写函数主体了
  找到PHP_FUNCTION(confirm_test_compiled)函数,该函数是测试函数,在该函数后面新写一个函数
  PHP_FUNCTION(hello)
  {
  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, "Hello,%s", arg);
  RETURN_STRINGL(strg, len, 0);
  }
  第三步:编译
  8、运行cmd命令行,进入d:\php_src\ext\linvo目录
  9、输入 msdev linvo.dsp /MAKE "linvo - Win32 Release_TS"
  10、如果没有错误,则在php_src目录下会生成一个Release_TS文件夹,里面就是编译好的php_linvo.dll扩展
  第四步:使用
  11、将其拷入运行环境中的php扩展目录ext
  12、编辑php.ini添加extension=php_linvo.dll,重启apache
  13、在php文件中执行如下语句 
  echo hello('Linvo');
  将输出
  Hello,Linvo
  14、通过echo phpinfo();也可看到扩展已加载的信息
  linvo
  linvo support enabled 三、加载问题
  当生成的扩展放到文件夹中,并且修改了php.ini之后有可能出现生成的扩展dll并没有被php加载,可以从以下几个方面进行检查。
  1、创建一个php,并且使用phpinfo()函数打出当前php信息,找到php中真实加载的php.ini。
  2、是否已经重启过apache。
  3、查看apache的错误日志,看出现的有没有关于加载扩展出错的一些信息。 如果出现"PHP Warning:  PHP Startup: extname: Unable to initialize module\nModule compiled with module API=20090626, debug=0, thread-safety=1\nPHP    compiled with module API=20060613, debug=0, thread-safety=1\nThese options need to match\n in Unknown on line 0"这样的提示,说明编译时使用源码版本与php的应用程序不符合(并未要求完全一致的版本) ,如果出现不符合。
  4、写扩展时是否引用了其它dll,对于此种问题原因,详见:
  http://www.guyzyl.com/post-37.html

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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 and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

See all articles