Home php教程 php手册 查看PHP opcode扩展模块Web服务

查看PHP opcode扩展模块Web服务

Jun 21, 2016 am 08:51 AM
php quot string

  最近花了大约一星期的时间写了一个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



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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 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.

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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

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.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 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.

See all articles