Use php-js-ext to parse javascript scripts in PHP _ PHP Tutorial

Source: Internet
Author: User
Use php-js-ext to parse javascript scripts in PHP. When you develop a PHP program for your personal interests and hobbies, do you have the energy to rewrite it with php in the face of js scripts that can achieve your goal but have more than a thousand lines? I want to use PHP to simulate user behavior. when you develop a PHP program for your personal interests, you can use JavaScript scripts that can achieve your goal with more than a thousand lines, do you have the energy to rewrite it with php? Do you want to use PHP to simulate user behavior? However, a site with js flood... an unknown php extension project can meet these needs. the purpose of this extension is to explain javascript in php.
Project Homepage Http://aurore.net/projects/php-js/
Php-js-ext is a bridge between mozilla javascript interpreter and php, so we need to download php-js-ext with the last version 0.1.2, you also need to download mozilla js with the latest version 1.7.0. the link is as follows (php-js-ext 0.1.2 works with js-1.5 when it is released, 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
Open js-1.7.0.tar.gz

Tar zxvf js-1.7.0.tar.gz
Cd js/src

Start compilation (if compilation cannot be completed, see the final description)

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 release/jsautocfg. h/usr/include/

Since then, the installation of mozilla js has been completed. The following describes how to install php-js-ext.
2. install php-js-ext
Open 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 extension version (if more than one php exists, enter the full path of phpize)

/Path/phpize

Configure and compile and install php (if more than one php exists or configure cannot find the php Location, you must specify-with-php-config =/path/php-config at configure)

./Configure
Make & make install

If everything is correct, js. so is already in your php lib Directory

Finally, modify your php. ini, add extension = js. so to the corresponding location, and confirm that the settings of extension_dir are correct.
Output a phpinfo result to check whether js. so is correctly loaded and possible error messages.
If everything is correct, we can start to use this function.
The instructions for using the official website are attached here:

A simple. /configure; make install shoshould 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 abve, 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 ";
Wocould 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.

(Non-Redhat linux releases may cause errors during mozilla js compilation because the connector and kernel are no longer correct.. out to provide support. we need to use gcc to generate a shared library instead of ld. Open js/src/config/Linux_All.mk and change MKSHLIB =$ (LD)-shared $ (XMKSHLIBOPTS) of row 50th to MKSHLIB =$ (CC)-shared $ (XMKSHLIBOPTS ), make-f Makefile. ref clean and then try to compile again .)


Why? I want to use PHP to simulate user behavior...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.