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

Source: Internet
Author: User
1. introduction the role of Erlang extended term format (Erlangexternaltermformat) 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 in binary format. 1. Introduction: Erlang extended term format (erlang external term format) 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, refer to the peb (Php-Erlang Bridge) extension to write this 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. this binary extension exports the following functions: array binary_to_term (string $ binary) returns the array after unpacking. 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. 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 On * nux: Step 1: --------- unzip binary.zip cd. /binaryphpize or phpize5 Step 2: --------- check the Erlang installation directory and php-config path before executing configure. The configure below is just an example and can be modified according to the actual path. Install :. /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-configOR :. /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 php. ini file: extension = binary. soOR: 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 extension example 4.1: CREATE a mysql data TABLE [SQL] 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 in Erlang [plain] 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 [php] GetOne ($ SQL); $ data = binary_to_term ($ bin); echo'
';      var_dump($data);      echo '
'; Output [php] 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 )}}}

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.