这天去zend网站上逛了逛, 看到一个monkeyspider 的标签,

http://devzone.zend.com/article/4704-Using-JavaScript-in-PHP-with-PECL-and-SpiderMonkey

嘿嘿, 原来是用c写了一个扩展php扩展 把spiderMonkey和php 联起来了。 照着试了试, 还真跑下来了, 拿上来分享一下。

 首先是下载

http://ftp.mozilla.org/pub/mozilla.org/js/  js-1.7   

http://pecl.php.net/package/spidermonkey   spidermonkey 的php扩展

  
  shell# tar -xzvf js-1.70.tar-gz
  shell# cd js/src
  shell# make -f Makefile.ref

  shell# mkdir -p /usr/local/include/js/
  shell# cp *.{h,tbl} /usr/local/include/js/
  shell# cd Linux_All_DBG.OBJ
  shell# cp *.h /usr/local/include/js/
  shell# cp js /usr/local/bin/
  shell# cp libjs.so /usr/local/lib/
  shell# ldconfig
 进入spidermonkey , php 扩展目录,
shell# phpize
shell# ./configure
shell# make
shell# make install

在make 的时候可能会报错, 那报错的那几行注释吧, 用了几个php 的struct 里没有的属性
在php.ini 里把这个扩展打开吧, 试试把
代码
<?php
// create JavaScript context
$js = new JSContext();
// define PHP variables
$a = 10;
$b = 2;

// assign variables to JavaScript context
$js->assign('a', $a);
$js->assign('b', $b);

// define script code
$script = <<<END
  c 
= a + b;
END;
// evaluate script and display result
echo "The sum of $a and $b is: " . $js->evaluateScript($script);
?>