search
HomeBackend DevelopmentPHP Tutorialwindows下配置php5.5开发环境及开发扩展_PHP

网上的教程是比较多的,但是我发现在windows下的扩展开发比较少,而且大多都是php5.3版本以前的,今天我就给大家讲解一下php扩展开发,我就拿php5.5的版本来说明一下的了

windows环境(我个人的)

代码如下:


windows 8.1 企业版(mac os Boot Camp安装的)
Visual Studio 2012 版本
msysgit  (http://msysgit.github.io/ 下载)
php-sdk-binary-tools-20110915.zip (http://windows.php.net/downloads/php-sdk/下载)
deps-5.5-vc11-x86.7z                  (http://windows.php.net/downloads/php-sdk/下载)
php-5.5.20                        (http://php.net/downloads.php下载,这个版本不定自己选择)

第一步
解压php-sdk-binary-tools的二进制包,譬如我解压到我的C:\php-sdk文件夹中,现在的目录结构如下

代码如下:


C:\php-sdk
    --bin
    --script
    --share

然后,这个是你已经安装完成了visual studio 2012,打开VS2012 Native Tools Command Prompt命令行工具

代码如下:


#进入目录
cd C:\php-sdk
#设置环境变量
bin\phpsdk_setvars.bat
#创建常用的php-sdk目录
bin\phpsdk_buildtree.bat phpdev

如果我们打开bin\phpsdk_buildtree.bat文件就会发现它只创建到VC9,没有VC11,但是我们如果开发php5.5版本,我们需要的是VC11,这时候我们就要把C:\php-sdk\phpdev\vc9复制一份到C:\php-sdk\phpdev\vc11,现在的目录结构如下:

代码如下:


C:\php-sdk\phpdev\
                --vc6
                --vc8
                --vc9
                --vc11

因为我下载的deps-5.5-vc11-x86.7z,所以我要解压deps-5.5-vc11-x86.7z到C:\php-sdk\phpdev\vc11\x86\deps文件夹下覆盖,里边都是我们需要的库文件和一些必要的工具等等。
然后,将我们下载的php-5.5.20.tar.bz2解压到C:\php-sdk\phpdev\vc11\x86\php-5.5.20文件夹中。

编译安装php

回到VS2012 Native Tools Command Prompt
#进入php源目录文件夹
cd c:\php-sdk\phpdev\vc11\x86\php-5.5.20
buildconf
#查看带的扩展和编译命令
configure --help
php-sdk

如果你没有安装php,现在这里就帮助到你了,可以先编译安装一下php
configure --disable-all --enable-cli
然后,你会看到Type 'nmake' to build PHP,然后编译
nmake
在C:\php-sdk\phpdev\vc11\x86\php-5.5.20\Release_TS文件夹下就生成了php.exe文件,环境变量中加入这个路径,好在命令行中能使用php命令。

开发PHP的第一个扩展

假如我们要开发一个widuu()函数,实现的效果如下

代码如下:


function widuu($string){
    return "your first extension {$string} is ok";
}

进入扩展目录,生成扩展文件夹

代码如下:


cd C:\php-sdk\phpdev\vc11\x86\php-5.5.20\ext
#输入php ext_skel_win32.php --extname=扩展名称
php ext_skel_win32.php --extname=widuuweb


这时候我们在C:\php-sdk\phpdev\vc11\x86\php-5.5.20\ext就看到了自己的目录widuuweb,打开widuuweb\php_widuuweb.h,在
#PHP_MINFO_FUNCTION(widuuweb);下一行写入自己的函数,譬如我定义的是widuu()函数
PHP_FUNCTION(widuu);
打开php_widuuweb.c编写函数,在PHP_FUNCTION(confirm_widuuweb_compiled)下

代码如下:


PHP_FUNCTION(widuu){
    char *arg_string = NULL;
    int  arg_len,str_len;
    char *string;
    if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"s",&arg_string,&arg_len) == FAILURE){
        return;
    }
    str_len = spprintf(&string,0,"your first extension %.78s is ok",arg_string);
    RETURN_STRINGL(string,str_len,0);
}

然后找到PHP_FE(confirm_widuuweb_compiled, NULL),在下边注册你的函数

代码如下:


const zend_function_entry widuuweb_functions[] = {
    PHP_FE(confirm_widuuweb_compiled,   NULL)       /* For testing, remove later. */
    PHP_FE(widuu,NULL)
    PHP_FE_END  /* Must be the last line in widuuweb_functions[] */
};

修改C:\php-sdk\phpdev\vc11\x86\php-5.5.20\ext\widuuweb\config.w32,去掉
// ARG_ENABLE("widuuweb", "enable widuuweb support", "no");
前边的注释,vs命令行,输入cd .. 到php-5.5.20目录中,输入下面的命令
buildconf --force
#查看扩展
configure --help
如果,有错误,大家可以打开,目录下configure.js查看,我这个出现了错误,是configure.js的4791行,
ARG_ENABLE("widuuweb", "enable widuuweb support", "no");  */
多了一个*/的注释,去掉就可以了,然后再输入
configure --help
就看到了,有一行
 --enable-widuuweb             enable widuuweb support
然后配置
configure --disable-all --enable-cli --enable-widuuweb=shared
php动态扩展

其中--enable-widuuweb=shared动态扩展,正好与静态扩展相对。
然后输入nmake,完成后,我们在C:\php-sdk\phpdev\vc11\x86\php-5.5.20\Release_TS目录下就发现了php_widuuweb.dll文件,可以放到环境中测试了。
如果你本地没有安装php环境,而是跟我一样只是编译了php,如果测试,我建议静态编译看效果
configure --disable-all --enable-cli --enable-widuuweb=static
nmake
然后,你可以再Release_TS目录下,输入
php -r "echo widuu('widuu');"  //输出"your first extension widuu is ok"
ok,就说到这里,以后再慢慢来讲,有什么不懂的大家可以留言,基础的ZEND_API,大家可以去walu.cc查看

Statement
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
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 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: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 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.

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.

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools