PHP extension module unpacks binary data in Erlangextterm format (generated by term_to_binary)

Source: Internet
Author: User
PHP extension module unpacks binary data in Erlangextterm format (generated by term_to_binary). 1. Introduction: Erlang extension term & #26684 has been introduced in Erlang's term_to_binary data packaging system; (Erlangexternaltermforma PHP extension module unpacks (generated by term_to_binary) binary data in Erlang ext term format
1. introduction the role of Erlang extended term format (Erlang external term format) binary has been introduced in Erlang's term_to_binary data packaging system,
Some students want to use the term_to_binary function package to store Erlang data into the database in binary format, and then use PHP to read and decompress the package into a PHP array.
To solve the problems encountered in the above application scenarios,
The peb (Php-Erlang Bridge) extension writes the binary extension similar to the erlang: binary_to_term/1 function,
It can be understood as the binary_to_term of PHP.

2. Introduction to binary extensions
2.1. the binary extension exports the following three functions:
  1. Array binary_to_term (string $ binary) returns the array after unpacking.
  2. Int binary_errno () returns the error code generated by the last call of binary_to_term. 0 indicates no error, and 1 indicates the unsupported data type in binary content.
  3. String binary_error () returns the error message generated by the last call of binary_to_term. If no error exists, it is null.
2.2. binary extension module:

Http://download.csdn.net/download/u011471961/6447631

3. install binary extension
Install On * nux:

Step 1:
---------
Unzip binary.zip
Cd./binary
Phpize or phpize5


Step 2:
---------
Before executing configure, check the Erlang installation directory and php-config path. The configure below is just an example and can be modified according to the actual path.
Install it in 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 ". /configure 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:
---------
Modify the php. ini file:
Extension = binary. so
OR:
Extension =/path/to/binary. so


Step 7:
---------
Restart php-cgi or apache


Step 8:
---------
Use the phpinfo () function or command (php-m) to check whether the binary module is loaded.


4. binary extended application example
4.1 create a mysql data table
CREATE TABLE IF NOT EXISTS `test` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `bin` varbinary(255) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

4.2 write data on Erlang
Test ()-> Data = [{name, rolong}, {phone, 888888}], Bin = term_to_binary (Data ), % the interface for writing data to the database depends on your own situation. db: execute (<"insert test (bin) values (~ S); ">>, [Bin]).

4.3 read and decompress the package in PHP into a PHP array
 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 the binary extension module, some of the less-used data types are currently supported. if you have special requirements, you can modify the source code.
Erlang provides a C interface for data packaging and unpacking, refer to http://www.erlang.org/doc/apps/erl_interface/index.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.