PHP擴充模組解包(由term_to_binary產生的)Erlang ext term格式的位元據

來源:互聯網
上載者:User
PHP擴充模組解包(由term_to_binary產生的)Erlang ext term格式的位元據

1、引言

《初探Erlang的term_to_binary資料封包機制》一文中已經介紹了Erlang擴充term格式(Erlang external term format)binary的作用,
有些同學想把Erlang資料通過term_to_binary函數封包後以二制進形式存入資料庫,然後用PHP讀取並解包成PHP數組。
為瞭解決上面的這種應用場合中遇到的問題,
參考peb(Php-Erlang Bridge)擴充寫了這個類似erlang:binary_to_term/1函數功能的binary擴充,
可以理解為是PHP版的binary_to_term。

2、binary擴充簡介


2.1、這個binary擴充匯出了以下三個函數:

  1. array binary_to_term(string $binary) 返回解包後的數組。
  2. int binary_errno () 返回上次調用binary_to_term產生的錯誤代號。0為無錯語,1為二進位內容中出現在不支援的資料類型。
  3. string binary_error () 返回上次調用binary_to_term產生的錯誤資訊,如果沒有出錯則為空白。

2.2、binary擴充模組下載地址:

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

3、binary擴充的安裝


Install On *nux:

Step 1:
---------
unzip binary.zip
cd ./binary
phpize或phpize5


Step 2:
---------
執行configure前先確認Erlang安裝目錄和php-config路徑,下面的configure只是樣本,可根據實際路徑修改。
在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
在蘋果電腦上安裝:
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:
---------
修改php.ini檔案:
extension=binary.so
OR:
extension=/path/to/binary.so


Step 7:
---------
重啟php-cgi或apache


Step 8:
---------
通過phpinfo()函數或命令(php -m)查看是否載入了binary模組。


4、binary擴充應用樣本


4.1、建立mysql資料表

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、在Erlang端寫入資料

test() ->    Data = [{name, rolong},{phone, 888888}],    Bin = term_to_binary(Data),    %% 寫入資料庫的介面依自己情況而定    db:execute(<<"insert test(bin) values(~s);">>, [Bin]).

4.3、在PHP中讀取並解包成PHP數組

getOne($sql);    $data = binary_to_term($bin);    echo '
';    var_dump($data);    echo '
';

輸出
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、小結

在binary擴充模組中,部分較少用的資料類型暫末支援,有特殊需要的同學可以在源碼中自行修改實現。
Erlang提供了資料封包、解包的C介面,可參考 http://www.erlang.org/doc/apps/erl_interface/index.html
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.