偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

使用php-js-ext在PHP內(nèi)解析javascript腳本

開發(fā) 后端
本文介紹了一個名為php-js-ext的php擴展項目。通過此擴展可以在php內(nèi)解釋javascript。文中作者介紹了在CentOS 4.5系統(tǒng)下安裝此擴展項目的步驟

當你在因個人興趣愛好而開發(fā)PHP程序時,面對可以達到你的目的卻長達千行以上的js腳本,你有精力去用php重寫它么?想用PHP模擬用戶行為么?但卻遇到j(luò)s泛濫的站點......一個不為人所知的php擴展項目可以滿足這些需求,這個擴展的目的就是在php內(nèi)解釋javascript。

項目主頁 http://aurore.net/projects/php-js/

php-js-ext是mozilla javascript解釋器和php之間的一座橋梁,因此我們除了需要下載***版本為0.1.2的php-js-ext,還需要下載***版本為1.7.0的mozilla js,鏈接如下(php-js-ext 0.1.2發(fā)布時是配合js-1.5工作,但經(jīng)我測試,也可以配合js-1.7工作)

wget http://aurore.net/projects/php-js/php-js-ext-0.1.2.tar.bz2
wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz

系統(tǒng)是CentOS 4.5

1.安裝mozilla js

解開js-1.7.0.tar.gz

tar zxvf js-1.7.0.tar.gz
cd js/src

開始編譯(如不能完成編譯見***說明)

make -f Makefile.ref

拷貝libjs.so到/usr/lib

cp Linux_All_DBG.OBJ/libjs.so /usr/lib
ldconfig

拷貝頭文件到/usr/include

cp jsapi.h jscompat.h jslong.h jsosdep.h jsotypes.h jspubtd.h jstypes.h jsproto.tbl jsconfig.h Linux_All_DBG.OBJ/jsautocfg.h /usr/include/

自此mozilla js的安裝工作就完成了,下面開始進行php-js-ext的安裝

2.安裝php-js-ext

解開php-js-ext-0.1.2.tar.bz2

tar jxvf php-js-ext-0.1.2.tar.bz2
cd php-js-ext-0.1.2

創(chuàng)造環(huán)境并設(shè)定擴展版本(如有一個以上的php存在,需要輸入phpize的完整路徑)

/path/phpize

配置并編譯安裝(如有一個以上的php存在或configure無法找到php的位置,需要在configure的時候指定-with-php-config=/path/php-config)

./configure
make && make install

如果一切無誤,js.so已經(jīng)在你的php的lib目錄

***,修改你的php.ini,在相應的位置加入extension=js.so,并確認extension_dir的設(shè)定正確。

輸出一個phpinfo的結(jié)果,以查看js.so是否被正確加載和可能的錯誤信息。
如果一切無誤的話,我們就可以開始使用這個功能了。

這里附上官方網(wǎng)站的使用說明:

A simple ./configure; make; make install should do the trick. Make sure to add an extension=js.so line to your php.ini/php.d. Note: you need to install libjs first. If you're using a Redhat-esque system, you can use the SRPM provided above, else, use the TBZ.

Then, just use js_eval to evaluate your JavaScript. js_eval returns the value returned by the JavaScript interpreter to PHP.

For example:

js_eval("var a = 123;");
js_eval("var b = 456;");

$c = js_eval("[a, b];");
echo "a is ".$c[0]."\n";
echo "b is ".$c[1]."\n";

js_eval("var sum = function(x, y) { return x + y; }");

$d = js_eval("sum(a, b);");
echo "The sum of a and b is ".$d."\n";

Would produce:

a is 123
b is 456
The sum of a and b is 579

js_eval takes an optional boolean argument, assoc, which returns objects as associative arrays instead of PHP objects.

The php-js execution environment provides two built-in JavaScript system functions:

* print
* gc

print outputs its argument to the php output stream.

gc forces garbage collection within the JavaScript environment.

(非Redhat的linux發(fā)行版可能會在mozilla js的編譯過程中產(chǎn)生錯誤,是因為連接器和內(nèi)核已經(jīng)不對a.out提供支持,我們需要用gcc來生成一個共享庫而不是ld了。打開js/src/config/Linux_All.mk,將第50行的 MKSHLIB = $(LD) -shared $(XMKSHLIBOPTS) 更改為 MKSHLIB = $(CC) -shared $(XMKSHLIBOPTS) ,make -f Makefile.ref clean后再次嘗試編譯。)

【編輯推薦】

  1. 在PHP中如何判斷AJAX請求
  2. PHP中的危險函數(shù)全解析
  3. 如何學習一個新的PHP框架
責任編輯:yangsai 來源: ChinaUnix論壇
相關(guān)推薦

2009-11-30 16:24:24

PHP腳本

2009-11-18 13:52:30

PHP shell腳本

2018-04-22 00:04:04

PHP C 代碼數(shù)據(jù)

2009-12-08 14:20:30

PHP CLI腳本

2011-07-12 17:11:13

PHPSHELL

2011-12-05 13:52:00

PHP

2009-09-22 11:54:42

ibmdwPHP

2011-05-25 13:22:05

PHPJSON

2016-08-11 09:48:53

2009-12-02 17:01:01

PHP隨機數(shù)rand()

2010-01-05 13:54:32

Jquery Json

2009-12-03 18:39:02

PHP變量php_se

2009-11-27 10:02:54

PHP mb_strw

2011-06-15 15:16:54

Session

2011-07-12 17:06:43

PHP

2020-03-07 18:51:11

EclipseFedoraPHP

2009-12-02 10:39:26

PHP DOMDocu

2009-11-25 10:02:27

PHP會話Sessio

2009-12-11 09:44:18

PHP創(chuàng)建PPT文檔

2009-12-08 17:29:26

PHP extends
點贊
收藏

51CTO技術(shù)棧公眾號