It took about a week to write a PHP extension module Opdumer and encapsulate it into a Web service (Click here to access it ). The main content of this module is to output the opcode corresponding to the PHP code. In fact, there have been some extension modules used to view opcode, such as the well-known vld. Of... "> <LINKhref =" http://www.php100.com//stat
It took about a week to write a PHP extension module Opdumer and encapsulate it into a Web service (Click here to access it ). The main content of this module is to output the opcode corresponding to the PHP code. In fact, there have been some extension modules used to view opcode, such as the well-known vld. The reason for re-implementing such a module is that vld does not support PHP_FUNCTION APIs, that is, vld can only be used in CLI format, while Opdumer has both CLI APIs and PHP_FUNCTION APIs, I also want to learn the opcode compilation and execution mechanism in Zend Engine with the opportunity of writing this module. I personally intend to write an article later on the opcode compilation and execution mechanism. This article mainly describes the usage of Opcode and the usage of corresponding Web services.
Opdumper
Install
The source code of Opdumper is hosted on github at https://github.com/ericzhang-cn/opdumper. Run the following command to clone the source code:
Git clone https://github.com/ericzhang-cn/opdumper.git
Opdumper is a standard PHP Extension. the installation method is as follows:
First, place the Opdumper source code to the ext/opdumper Directory of the PHP source code package. go to this directory and execute the following command:
Phpize
./Configure
Make
Make install
Then add a line of configuration in php. ini:
Extension = opdumper. so
Currently, opdumper supports PHP> = 5.3. it is tested in Linux and MacOS, but not in Windows.
CLI API
Opdumper supports outputting opcode in a command line similar to vld. you only need to pass opdumper. active = 1 through the-d parameter when executing the php command. For example, we have foo. php:
$ A = 'hello ';
Echo $;
?>
Run the following command:
Php-d opdumper. active = 1 foo. php
The result is as follows:
PHP_FUNCTION API
Opdumper also supports PHP_FUNCTION APIs not supported by vld. Opdumper provides two PHP functions: od_dump_opcodes_string and od_dump_opcodes_file. The former accepts a string for production, and the string is a piece of PHP code. The latter accepts a PHP file as a parameter, and the return value is a PHP array containing the opcode result. Take od_dump_opcodes_file as an example. we can write another bar. php file in the same directory of foo. php:
$ Opcodes = od_dump_opcodes_file ('./foo. php ');
Var_dump ($ opcodes );
?>
The execution result is as follows:
Array (3 ){
[0] =>
Array (8 ){
["Lineno"] =>
Int (2)
["Opcode"] =>
String (11) "ZEND_ASSIGN"
["Opdomaintype"] =>
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"
["Opdomaintype"] =>
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"
["Opdomaintype"] =>
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's Web service: Opcode Dumper
Frankly speaking, installing the PHP module is still quite troublesome. So to make it easier for my friends to view opcode, I set up an online Web service for Opdumper: http://supercompiler.com/app/opcode_dumper.
Web page access
You only need to access this page and enter or paste the PHP code in the edit box to quickly view the corresponding opcode:
You can also download the results to a local directory (in CSV format ).
Http api access
You can access the following API to obtain the opcode of the PHP code:
URI: http://supercompiler.com/api/dump_opcodes
Method: POST
Params: php_script = [your PHP code]
The returned value is in JSON format. when the request succeeds, the success field is "true", the data field is stored in opcodes, the success field is "false" when the request fails, and the reason why the msg field fails to be stored.
Due to the relationship that spans, you can only use Curl instead of Ajax to call this API. the JSONP interface will be added later.
Conclusion
At present, this module is still relatively elementary and needs to be improved in many aspects. You are also welcome to contribute code through github.
Link: http://www.codinglabs.org/html/opdumper-and-web-opcode-dumper.html