Home > Backend Development > PHP Tutorial > 在php 中运行js

在php 中运行js

WBOY
Release: 2016-06-23 14:33:06
Original
956 people have browsed it

这天去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
Copy after login

进入spidermonkey , php 扩展目录,

shell# phpizeshell# ./configureshell# makeshell# make install
Copy after login

在make 的时候可能会报错, 那报错的那几行注释吧, 用了几个php 的struct 里没有的属性在php.ini 里把这个扩展打开吧, 试试把
Copy after login
              <p class="sycode">                      代码                  <p class="sycode">      <p class="sycode">       <? 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 ); ?>      </p>     </p>              </p><p> </p>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template