查看PHP opcode扩展模块Web服务
最近花了大约一星期的时间写了一个PHP扩展模块Opdumer,并封装成了Web服务(点击这里访问)。这个模块的主要内容是输出PHP代码对应的opcode。其实之前已经有一些用于查看opcode的扩展模块,如比较有名的vld。之所以重新实现一个这样的模块,主要是因为vld不支持PHP_FUNCTION API,也就是说vld只能用于CLI形式,而Opdumer同时拥有CLI API和PHP_FUNCTION API,另外,也想借助编写这个模块的机会学习Zend Engine中opcode的编译和执行机制。个人打算后面专门针对opcode的编译执行机制写一篇文章,而本文主要描述Opcode的使用方法及对应Web服务的使用。
Opdumper
安装
Opdumper的源码已经托管在github上,其地址为:https://github.com/ericzhang-cn/opdumper。可以通过以下命令克隆源代码:
git clone https://github.com/ericzhang-cn/opdumper.git
Opdumper是一个标准的PHP Extension,安装方法如下:
首先将Opdumper源码放到PHP源码包的ext/opdumper目录下,进入此目录执行如下命令:
phpize
./configure
make
make install
然后在php.ini中添加一行配置:
extension=opdumper.so
目前opdumper支持PHP>=5.3,在Linux和MacOS下测试通过,Windows下未做测试。
CLI API
Opdumper支持类似vld的命令行方式输出opcode,只需在执行php命令时通过-d参数将opdumper.active=1传入。例如我们有一个foo.php:
$a = 'hello';
echo $a;
?>
执行如下命令:
php -d opdumper.active=1 foo.php
结果如下:
PHP_FUNCTION API
Opdumper还支持vld不支持的PHP_FUNCTION API,Opdumper提供了两个PHP函数:od_dump_opcodes_string和od_dump_opcodes_file。前者接受一个字符串作为产生,字符串是一段PHP代码;后者接受一个PHP文件作为参数,返回值均是一个存有opcode结果的PHP数组。以od_dump_opcodes_file为例,我们在foo.php同一目录下再写一个bar.php:
$opcodes = od_dump_opcodes_file('./foo.php');
var_dump($opcodes);
?>
执行结果如下:
array(3) {
[0]=>
array(8) {
["lineno"]=>
int(2)
["opcode"]=>
string(11) "ZEND_ASSIGN"
["op1_type"]=>
string(2) "CV"
["op2_type"]=>
string(5) "CONST"
["result_type"]=>
string(0) ""
["op1"]=>
string(2) "~0"
["op2"]=>
string(5) "hello"
["result"]=>
string(0) ""
}
[1]=>
array(8) {
["lineno"]=>
int(3)
["opcode"]=>
string(9) "ZEND_ECHO"
["op1_type"]=>
string(2) "CV"
["op2_type"]=>
string(6) "UNUSED"
["result_type"]=>
string(6) "UNUSED"
["op1"]=>
string(2) "~0"
["op2"]=>
string(6) "UNUSED"
["result"]=>
string(6) "UNUSED"
}
[2]=>
array(8) {
["lineno"]=>
int(5)
["opcode"]=>
string(11) "ZEND_RETURN"
["op1_type"]=>
string(5) "CONST"
["op2_type"]=>
string(6) "UNUSED"
["result_type"]=>
string(6) "UNUSED"
["op1"]=>
string(1) "1"
["op2"]=>
string(6) "UNUSED"
["result"]=>
string(6) "UNUSED"
}
}
Opdumper的Web服务:Opcode Dumper
坦白说,安装PHP模块还是挺麻烦的。所以为了方便朋友们查看opcode,我为Opdumper搭建了一个在线Web服务:http://supercompiler.com/app/opcode_dumper。
Web页面访问
只要访问这个页面,在编辑框中输入或粘贴进PHP代码,就可以快速看到相应的opcode:
同时,也可以将结果下载到本地(CSV文件格式)。
HTTP API方式访问
您可以通过访问如下API获取PHP代码的opcode:
URI: http://supercompiler.com/api/dump_opcodes
Method: POST
Params: php_script=[您的PHP代码]
返回值为JSON格式,成功时success字段为”true”,data字段存储opcodes;失败时success字段为”false”,msg字段存放失败原因。
由于跨越的关系,目前只能使用Curl而不能使用Ajax方式调用这个API,后续会为其增加JSONP接口。
结语
目前这个模块还比较初级,有很多需要完善的地方。也欢迎有兴趣的朋友通过github贡献代码。
原文链接:http://www.codinglabs.org/html/opdumper-and-web-opcode-dumper.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
