简体   繁体   中英

Read server-side JavaScript from PHP

I have a JS library file of variables (caption strings) used by server-side JavaScript. Can I read/interpret that from PHP?

The path to the data isn't the issue, but whether PHP can read/use the JS variables. Currently I've the same variable stored in both a JS file and PHP include with obvious scope for changes getting out of synch. I've tried reading the PHP from the server-side JS without joy but was wondering if - with suitable parsing - if PHP could extract/use the JS variables.

I repeat all data involved is server-side on the same server. Sorry if I've missed this being answered before but PHP/JS questions are (even if mis-titled) seemingly all about passing data between client and server side processes - which isn't my scenario.

Later - clarification:

The s/s JavaScript is part of a web interface to an image database where I can't alter the API. It doesn't support creating emails (for file requests) thus I am having to do that via PHP. Work scope/budget precludes re-writing a new d/b interface, rather we 'just' ned to be able to send an email. IOW, I realise you wouldn't choose to be trying to do this if starting from scratch! Also, I don't think I can do s/side AJAX to fetch variables or do PHP smtp mail as the JS environment seemingly isn't designed for that degree of s/s processing pre HTML page delivery.

PHP可以通过V8js库与JavasScript交互

It sounds like you have config values (ie the captions) in the JS code.

If you want to keep it this way, my suggestion would be to split the config itself into a discrete .JS file, which would contain just the variable definitions but no actual JS code.

Then you should be able to simply parse it in PHP using json_decode() . Job done.

Depends on the format. If your file is anywhere near to JSON and publicly accessible (or at least on the same server with PHP), you could do something like:

File test.js :

var data = {
    'Hello': 'L-ghodwa it-tajba',
    'Bye':   'Caw',
}

File reader.php :

<?php

    $data = file_get_contents('test.js'); // or http://url.to/test.js
    $data = str_replace('var data = ', '', $data);
    $data = json_decode($data);
    print_r($data);

?>

This system is also pretty efficient; it is about manipulating a string a little bit and parsing it as JSON instead of having a full-blown JS parser. However, you have to enforce the format of the js file.

Edit: Yea, well, seems Spudley got first a few seconds before mine. Basically, I'm doing what he suggested.

Edit 2: I've been thinking, you can simply make use of JSON files. These must contain JSON, so the format is already there; and you can use the same file through JS and PHP without problems or afterthoughts. JSON files end with a .json extension, by the way. You might need to tell your webserver what mimetype json is, using (Apache/htaccess):

AddType application/json .json

I suppose that most answer in the client/server case involves JSON. What is stopping you from using the same method, albeit with the js script running on the server?

You could store these variables in a file in JSON, and have both the JS script and the PHP script load them. Of course, your PHP installation must support JSON to begin with.

You should be using either pretty complex regular expressions if you are trying to parse a JS file in PHP, or you should be creating an API which will allow communicating between JS scripts (eg in Node) and PHP.

Question is, why do you need to do this in exact this fashion? Why can't you share data in a format that can be used by both JS and PHP? JSON, for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM