Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use php-js-ext to parse javascript scripts within PHP

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use php-js-ext to parse javascript scripts in PHP, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

When you are developing a PHP program for your own interests, do you have the energy to rewrite it with php in the face of js scripts that can achieve your goal but are more than a thousand lines long? Want to use PHP to simulate user behavior? But encountered sites flooded with js. An unknown php extension project can meet these requirements, and the purpose of this extension is to explain javascript within php.

Project home page http://aurore.net/projects/php-js/

Php-js-ext is a bridge between mozilla javascript interpreter and php, so we not only need to download php-js-ext with version 0.1.2 of * *, but also need to download mozilla js with version 1.7.0 of * *. The link is as follows (php-js-ext 0.1.2 works with js-1.5 when released, but it can also work with js-1.7 after my test)

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 (if you can't finish the compilation, see * * instructions)

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, so let's start the installation of php-js-ext

two。 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 the environment and set the extended version (if more than one php exists, you need to enter the full path of the phpize)

/ path/phpize

Configure and compile the installation (if more than one php exists or configure cannot find the location of the 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's lib directory

*, modify your php.ini, add extension=js.so in the appropriate location, and make sure that the extension_dir setting is correct.

Output the result of a phpinfo to see if the js.so is loaded correctly and possible error messages.

If everything is all right, we can start using this feature.

Instructions for the use of the official website are attached here:

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 "an 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 an and b is". $D. "\ n"

Would produce:

An is 123

B is 456

The sum of an 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 distributions may cause errors during mozilla js compilation because connectors and kernels no longer support a.out, and 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 and try to compile again.)

The above is how to use php-js-ext to parse javascript scripts in PHP. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report