Home >Backend Development >PHP Tutorial >Parse javascript scripts within PHP using php-js-ext_PHP Tutorial
When you develop a PHP program for your personal interests and hobbies, and face a js script that can achieve your purpose but is more than a thousand lines long, do you have the energy to rewrite it in php? Want to simulate user behavior with PHP? But I encountered a site flooded with js... A little-known php extension project can meet these needs. The purpose of this extension is to interpret javascript in php.
Project homepagehttp://aurore.net/projects/php-js/
php-js-ext is a bridge between mozilla javascript interpreter and php, so we need Download the last version of php-js-ext which is 0.1.2. You also need to download the latest version of mozilla js which is 1.7.0. The link is as follows (php-js-ext 0.1.2 was released to work with js-1.5, but after my Test, can also work with 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
The system is CentOS 4.5
1. Install mozilla js
unlock js-1.7.0.tar.gz
tar zxvf js-1.7.0.tar.gz
cd js /src
Start compilation (see the final instructions if compilation cannot be completed)
make -f Makefile.ref
Copy libjs.so To /usr/lib
cp Linux_All_DBG.OBJ/libjs.so /usr/lib
ldconfig
Copy the header file to /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/
Since then, the installation of mozilla js has been completed. Let’s start the installation of php-js-ext
2. Install php-js-ext
Unlock 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
Create an environment and set the extended version (if more than one php exists, you need to enter the full path to phpize)
/path/phpize
Configure, compile and install (if more than one PHP exists or configure cannot find the location of PHP, you need to specify -with-php-config=/path/php-config when configure)
./configure
make && make install
If everything is correct, js.so is already in your php lib directory
Finally, modify your php.ini, in the corresponding Add extension=js.so to the location and confirm that extension_dir is set correctly.
Output a phpinfo result to see if js.so is loaded correctly and possible error messages.
If everything is correct, we can start using this feature.
Attached here are the instructions from the official website:
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. The error occurs because the connector and kernel no longer support a.out. We need to use gcc to generate a shared library instead of ld. Open js/src/config/Linux_All.mk, change MKSHLIB = $(LD) -shared $(XMKSHLIBOPTS) on line 50 to MKSHLIB = $(CC) -shared $(XMKSHLIBOPTS), make -f Makefile.ref clean Then try compiling again. )