简体   繁体   English

从远程页面获取JavaScript变量?

[英]Get a JavaScript variable from a remote page?

I am currently working on developing an API for a company. 我目前正在为公司开发API。 Specifically, here's my issue. 具体来说,这是我的问题。 They have JavaScript arrays on a webpage that their webmaster updates. 他们在网站管理员要更新的网页上具有JavaScript数组。 I have to pull these arrays into either a simple JS script or PHP file and get the contents of these arrays, which I can then arrange according to the API's specifications and output it as JSON. 我必须将这些数组拉入一个简单的JS脚本或PHP文件中,并获取这些数组的内容,然后可以根据API的规范进行排列并将其输出为JSON。

How do I pull a JavaScript variable in from a remote page in either PHP or jQuery/JS and make it usable for other applications? 如何从PHP或jQuery / JS的远程页面中提取JavaScript变量并使之可用于其他应用程序?

No, I don't have access to the company's website. 不,我无权访问该公司的网站。 I have to work off of page scraping for this one. 我必须解决这一问题。

Thank you! 谢谢!

You can't access private javascript variables remotely, due to Same origin policy . 由于同源来源政策 ,您无法远程访问私有javascript变量。 They would have to output the arrays in some kind of readable format that you could access using AJAX, probably as JSON. 他们将不得不以某种可读格式输出数组,您可以使用AJAX来访问它们,可能是JSON。

Edit: As mentioned below, if the array is explicitly defined as text in a javascript file, you could grab the contents of that file using cURL in PHP 编辑:如下所述,如果数组在javascript文件中被明确定义为文本,则可以使用PHP中的cURL来获取该文件的内容

如果我在哪里,我将使用PHP来file_get_contents (或CURL具体取决于服务器配置)页面,然后根据任何标记对其进行解析,以找到变量的值,并假设变量已写到页面中。第一名。

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

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