PHP Extension Module unpacking binary data in Erlang ext term format (generated by term_to_binary)
1. Introduction
The article "Exploring the Term_to_binary data encapsulation mechanism of Erlang" has introduced the role of Erlang Extended term format (Erlang external term format) binary,
Some students want to term_to_binary the Erlang data into the database in two-in-a-way and then use PHP to read and unpack it into a PHP array.
In order to solve the problems encountered in this application,
The reference peb (Php-erlang Bridge) extension writes this binary extension similar to the function of the ERLANG:BINARY_TO_TERM/1 function,
Can be understood as a PHP version of the binary_to_term.
2. Introduction to Binary extensions
2.1. This binary extension exports the following three functions:
- The array binary_to_term (string $binary) returns the unpacked arrays.
- int Binary_errno () returns the error code generated by the last call to Binary_to_term. 0 is an error-free language, and 1 is a binary content that is not supported in the data type.
- String Binary_error () returns the error message generated by the last call to Binary_to_term, or null if there is no error.
2.2. binary extension Module download address:
http://download.csdn.net/download/u011471961/6447631
3, binary expansion of the installation
Install on *nux:
Step 1:
---------
Unzip Binary.zip
CD./binary
Phpize or Phpize5
Step 2:
---------
Before executing configure, confirm the Erlang installation directory and Php-config path, the following configure is just an example and can be modified according to the actual path.
Install under Linux:
./configure cc= "Gcc-l/usr/local/lib/erlang/lib/erl_interface-3.7.11/lib-i/usr/local/lib/erlang/lib/erl_ Interface-3.7.11/include "--enable-binary--with-php-config=/usr/local/php/bin/php-config
OR:
./configure cc= "Gcc-l/usr/local/lib/erlang/lib/erl_interface-3.7.11/lib-i/usr/local/lib/erlang/lib/erl_ Interface-3.7.11/include "--enable-binary--with-php-config=/usr/bin/php-config5
Install on an Apple computer:
sudo macosx_deployment_target=10.7 cflags= "-arch i386-arch x86_64-g-os-pipe-no-cpp-precomp" CCFLAGS= "-arch i386-arch X86_64-g-os-pipe "cxxflags="-arch i386-arch x86_64-g-os-pipe "ldflags="-arch i386-arch x86_64-bind_at_load "./co Nfigure cc= "Gcc-m32-l/usr/local/lib/erlang/lib/erl_interface-3.7.6/lib-i/usr/local/lib/erlang/lib/erl_ Interface-3.7.6/include "--enable-binary
Step 3:
---------
Make
Step 4:
---------
Make Test
Step 5:
---------
Make install
Step 6:
---------
To modify the php.ini file:
Extension=binary.so
OR:
Extension=/path/to/binary.so
Step 7:
---------
Restart php-cgi or Apache
Step 8:
---------
See if the binary module is loaded with the phpinfo () function or command (PHP-M).
4. binary Extension Application Example
4.1. Create MySQL Data sheet
CREATE TABLE IF not EXISTS ' test ' ( ' id ' int (one) not null auto_increment, ' bin ' varbinary (255) is not NULL, PRIMA RY KEY (' id ')) engine=innodb DEFAULT Charset=utf8 auto_increment=1;
4.2. Write data on the Erlang side
Test (), data = [{name, Rolong},{phone, 888888}], Bin = term_to_binary (data), the interface to write to the database depends on its own circumstances Db:execute (<< Insert Test (BIN) values (~s); >>, [Bin]).
4.3. Read and unpack PHP arrays in PHP
GetOne ($sql); $data = Binary_to_term ($bin); Echo ''; Var_dump ($data); Echo '
';
Output
Array (1) { [0]=> Array (2) { [0]=> Array (2) { [0]=> string (4) "Name" [1]=> string (6) "Rolong" } [1]=> Array (2) { [0]=> string (5) "Phone" [1]=> Int (888888) }}}
4. Summary
In binary extension module, some of the less used data types are temporarily supported, and students with special needs can modify their own implementation in the source code.
Erlang provides a C interface for data encapsulation and unpacking, which can be referenced in http://www.erlang.org/doc/apps/erl_interface/index.html