简体   繁体   English

从PHP读取服务器端JavaScript

[英]Read server-side JavaScript from PHP

I have a JS library file of variables (caption strings) used by server-side JavaScript. 我有一个服务器端 JavaScript使用的变量(字幕字符串)的JS库文件。 Can I read/interpret that from PHP? 我可以从PHP中读取/解释它吗?

The path to the data isn't the issue, but whether PHP can read/use the JS variables. 数据的路径不是问题,但PHP是否可以读取/使用JS变量。 Currently I've the same variable stored in both a JS file and PHP include with obvious scope for changes getting out of synch. 目前我存储在JS文件和PHP中的相同变量包含明显的变化范围,使得变化不同步。 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. 我已经尝试从服务器端 JS中读取PHP而没有高兴但是想知道是否 - 通过适当的解析 - 如果PHP可以提取/使用JS变量。

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. 很抱歉,如果我之前错过了这个问题,但PHP / JS问题(即使是错误标题)似乎都是关于在客户端和服务器端进程之间传递数据 - 这不是我的情况。

Later - clarification: 后来 - 澄清:

The s/s JavaScript is part of a web interface to an image database where I can't alter the API. s / s JavaScript是图像数据库的Web界面的一部分,我无法改变API。 It doesn't support creating emails (for file requests) thus I am having to do that via PHP. 它不支持创建电子邮件(用于文件请求),因此我必须通过PHP来实现。 Work scope/budget precludes re-writing a new d/b interface, rather we 'just' ned to be able to send an email. 工作范围/预算不能重写新的d / b界面,而是我们只需要能够发送电子邮件。 IOW, I realise you wouldn't choose to be trying to do this if starting from scratch! IOW,我意识到如果从头开始,你不会选择尝试这样做! 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. 此外,我不认为我可以使用s / side AJAX来获取变量或执行PHP smtp邮件,因为JS环境似乎不是为HTML页面传递之前的s / s处理程度而设计的。

PHP可以通过V8js库与JavasScript交互

It sounds like you have config values (ie the captions) in the JS code. 听起来你在JS代码中有配置值(即字幕)。

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. 如果你想保持这种方式,我的建议是将配置本身拆分成一个离散的.JS文件,它只包含变量定义但不包含实际的JS代码。

Then you should be able to simply parse it in PHP using json_decode() . 然后你应该能够使用json_decode()在PHP中简单地解析它。 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: 如果您的文件接近JSON并且可以公开访问(或者至少在与PHP相同的服务器上),您可以执行以下操作:

File test.js : 文件test.js

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

File reader.php : 文件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. 它是关于稍微操纵一个字符串并将其解析为JSON而不是拥有一个完整的JS解析器。 However, you have to enforce the format of the js file. 但是,您必须强制执行js文件的格式。

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. 编辑2:我一直在想,你可以简单地使用JSON文件。 These must contain JSON, so the format is already there; 这些必须包含JSON,因此格式已经存在; and you can use the same file through JS and PHP without problems or afterthoughts. 并且您可以通过JS和PHP使用相同的文件而不会出现问题或事后发生。 JSON files end with a .json extension, by the way. 顺便说一句,JSON文件以.json扩展名结尾。 You might need to tell your webserver what mimetype json is, using (Apache/htaccess): 您可能需要使用(Apache / htaccess)告诉您的网络服务器mimetype json是什么:

AddType application/json .json

I suppose that most answer in the client/server case involves JSON. 我想客户端/服务器案例中的大多数答案都涉及JSON。 What is stopping you from using the same method, albeit with the js script running on the server? 什么阻止你使用相同的方法,虽然服务器上运行的js脚本?

You could store these variables in a file in JSON, and have both the JS script and the PHP script load them. 您可以将这些变量存储在JSON文件中,并让JS脚本和PHP脚本加载它们。 Of course, your PHP installation must support JSON to begin with. 当然,您的PHP安装必须首先支持JSON。

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. 如果你试图用PHP解析JS文件,你应该使用相当复杂的正则表达式,或者你应该创建一个允许JS脚本(例如Node)和PHP之间进行通信的API。

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? 为什么不能以JS和PHP都可以使用的格式共享数据? JSON, for example. 例如,JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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