使用php-js-ext在PHP內解析javascript指令碼_PHP教程

來源:互聯網
上載者:User
當你在因個人興趣愛好而開發PHP程式時,面對可以達到你的目的卻長達千行以上的js指令碼,你有精力去用php重寫它嗎?想用PHP類比使用者行為嗎?但卻遇到js泛濫的網站......一個不為人所知的php擴充項目可以滿足這些需求,這個擴充的目的就是在php內解釋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發布時是配合js-1.5工作,但經我測試,也可以配合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
系統是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

創造環境並設定擴充版本(如有一個以上的php存在,需要輸入phpize的完整路徑)

/path/phpize

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

./configure
make && make install

如果一切無誤,js.so已經在你的php的lib目錄

最後,修改你的php.ini,在相應的位置加入extension=js.so,並確認extension_dir的設定正確。
輸出一個phpinfo的結果,以查看js.so是否被正確載入和可能的錯誤資訊。
如果一切無誤的話,我們就可以開始使用這個功能了。
這裡附上官方網站的使用說明:

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發行版可能會在mozilla js的編譯過程中產生錯誤,是因為連接器和核心已經不對a.out提供支援,我們需要用gcc來產生一個共用庫而不是ld了。開啟js/src/config/Linux_All.mk,將第50行的 MKSHLIB = $(LD) -shared $(XMKSHLIBOPTS) 更改為 MKSHLIB = $(CC) -shared $(XMKSHLIBOPTS) ,make -f Makefile.ref clean後再次嘗試編譯。)


http://www.bkjia.com/PHPjc/446651.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446651.htmlTechArticle當你在因個人興趣愛好而開發PHP程式時,面對可以達到你的目的卻長達千行以上的js指令碼,你有精力去用php重寫它嗎?想用PHP類比使用者行為...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.