简体   繁体   English

使用javascript将DOM对象发送到PHP服务器

[英]Send DOM object to PHP server with javascript

I'm working on a chrome extension that sends the source code of a page to a server where it should be parsed. 我正在开发一个chrome扩展程序,它将页面的源代码发送到应该对其进行解析的服务器。

Capturing the source code is working fine, if I display it in the console, it looks like this: 捕获源代码工作正常,如果我在控制台中显示它,则它看起来像这样:

在此处输入图片说明

Then in order to push it to my PHP server, I first isolate the content of the body (what you've seen in the previous picture is stored in "result"): 然后,为了将其推送到我的PHP服务器,我首先隔离了正文的内容(您在上图中看到的内容存储在“结果”中):

html_content = result.querySelectorAll('body')[0].outerHTML;
html_content =JSON.stringify (html_content);

If I then display html_content in my console, I get something like this: 如果然后在控制台中显示html_content,则会得到以下信息:

在此处输入图片说明

So now that I have a JSON object, I try to send it through this: 因此,现在有了JSON对象,我尝试通过此对象发送它:

var xhr = new XMLHttpRequest(); 
xhr.open("POST", "myAPI_URL");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(html_content);

The call to the url works but I don't get anything in $_POST. 对该网址的调用有效,但是我在$ _POST中什么都没得到。 It's empty 它是空的

If I try to assign a specific variable like this: 如果我尝试分配这样的特定变量:

xhr.send('content='+html_content);

It doesn't work either. 它也不起作用。 On the PHP side, I'm just doing this: 在PHP方面,我只是这样做:

print_r($_POST);

And this returns an empty array. 这将返回一个空数组。

======= UPDATE ========= =======更新=========

Based on the feedback below, I adapted a few things and it gets better. 根据下面的反馈,我进行了一些调整,使其变得更好。 As suggested I'm using text/plain and I keep the DOM object intact (I don't take only the body) 如建议的那样,我正在使用文本/纯文本,并且使DOM对象保持完整(我不只接受主体)

            var xhr = new XMLHttpRequest(); 
            xhr.open("POST", "myAPI URL");
            xhr.setRequestHeader("Content-Type", "text/plain");
            xhr.send(content);

If I use this on the server side: 如果我在服务器端使用此功能:

$html_content = file_get_contents('php://input');

This variable contains the text string as expected so that's great but now if I try to parse the received html, it goes wrong. 此变量包含预期的文本字符串,因此效果很好,但现在如果我尝试解析接收到的html,则会出错。

$html_content = file_get_contents('php://input');
$dom = new DOMDocument;
$dom->loadHTML($html_content);

When doing this I get warnings like 这样做时,我会收到类似的警告

<b>Warning</b>:  DOMDocument::loadHTML(): ID ghostery-no-tracker already defined in Entity, line: 506 in <b> my url </b> on line <b>25</b><br />

It's like it doesn't understand the html correctly. 好像它无法正确理解html。

Any idea? 任何想法?

Martin commented (almost) correctly 马丁(几乎)正确评论

one can get the posted data with php://input ... $_POST fields do require a form. 可以使用php://input获取发布的数据。 $_POST字段确实需要一种形式。

PHP does not populate the superglobal if the the Content-Type is not one of the form-data content types. 如果Content-Type不是表单数据内容类型之一,则PHP不会填充超全局变量。 If think the reason behind this is simply because this is the only format implemented in PHP to map values to keys. 如果认为这背后的原因仅仅是因为这是PHP中实现的唯一将值映射到键的格式。

But the data is still there! 但是数据仍然存在!

You can read it from php://input , or (even better:) directly from STDIN which is a constant with an open stream to the former destination. 您可以从php://input读取,也可以直接从STDIN (甚至更好:)读取它, STDIN是一个常量,其中包含到先前目标的开放流。

http://php.net/manual/en/wrappers.php.php http://php.net/manual/en/wrappers.php.php

Do not use $HTTP_RAW_POST_DATA as it is deprecated / removed. 不要使用$HTTP_RAW_POST_DATA因为它已被弃用/删除。

Update 更新

Please do only ask one question per thread, especially if the two things are not related. 请仅在每个线程中问一个问题,特别是如果这两个问题无关。

DOMDocument shows the warnings not because it doesn't understand the HTML but because the HTML is buggy ;-) DOMDocument显示警告不是因为它不理解HTML,而是因为HTML有问题;-)

It's up to you how to handle the warnings, if you ignore them or fix the input. 如果忽略警告或修复输入,则取决于您如何处理警告。 Do not expect DOMDocument to be as forgiving as a modern browser. 不要期望DOMDocument像现代浏览器一样宽容。

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

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