自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

項(xiàng)目主頁 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ā)布時(shí)是配合js-1.5工作,但經(jīng)我測(cè)試,也可以配合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的安裝工作就完成了,下面開始進(jìn)行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è)定擴(kuò)展版本(如有一個(gè)以上的php存在,需要輸入phpize的完整路徑)

/path/phpize

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

./configure
make && make install

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

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

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

這里附上官方網(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ā)行版可能會(huì)在mozilla js的編譯過程中產(chǎn)生錯(cuò)誤,是因?yàn)檫B接器和內(nèi)核已經(jīng)不對(duì)a.out提供支持,我們需要用gcc來生成一個(gè)共享庫而不是ld了。打開js/src/config/Linux_All.mk,將第50行的 MKSHLIB = $(LD) -shared $(XMKSHLIBOPTS) 更改為 MKSHLIB = $(CC) -shared $(XMKSHLIBOPTS) ,make -f Makefile.ref clean后再次嘗試編譯。)

【編輯推薦】

  1. 在PHP中如何判斷AJAX請(qǐng)求
  2. PHP中的危險(xiǎn)函數(shù)全解析
  3. 如何學(xué)習(xí)一個(gè)新的PHP框架
責(zé)任編輯: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

2016-08-11 09:48:53

2011-05-25 13:22:05

PHPJSON

2010-01-05 13:54:32

Jquery Json

2009-12-02 17:01:01

PHP隨機(jī)數(shù)rand()

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

2020-03-07 18:51:11

EclipseFedoraPHP

2009-11-25 10:02:27

PHP會(huì)話Sessio

2011-07-12 17:06:43

PHP

2009-12-02 10:39:26

PHP DOMDocu

2009-12-02 18:43:35

PHP腳本編寫

2017-03-07 09:05:05

JavaScriptJavaPHP
點(diǎn)贊
收藏

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