简单的HTML DOM解析器-发送帖子变量 [英] Simple HTML DOM Parser - Send post variables
问题描述
我有用于PHP的简单HTML DOM解析器,并且正在使用以下标记:
I have the Simple HTML DOM Parser for PHP, and I am using the following markup:
$html = file_get_html('http://www.google.com');
但是,我如何将发布变量(如cURL)发送到该页面并获得响应?例如
However how do I send post variables (like a cURL) to that page and get the response? For example
$html = file_get_html('http://www.google.com', array("Item"=>"Value", "Item2"=>"Value2"));
推荐答案
据我所知,该文档没有提及它,但是在查看源代码后,我注意到您正在使用的函数接受一个流上下文作为其第三个参数.您可以使用以下PHP功能创建发布请求:
The documentation doesn't mention it as far as I can see, but after taking a look in the source code I noticed the function you're using accepts a stream context as its third argument. You can create a post request with this PHP feature like this:
$request = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query(array(
'Item' => 'Value',
'Item2' => 'Value2'
)),
)
);
$context = stream_context_create($request);
$html = file_get_html('http://www.google.com', false, $context);
如果您不喜欢上下文或希望使用其他方法(例如cURL扩展名),则还可以使用该方法获取页面内容,然后使用str_get_html()
或$parser->load()
将其提供给解析器;该类本身在内部与您现在使用的方法几乎相同.
If you don't like contexts or would prefer a different method (like the cURL extension) you could also just fetch the page content using that, then feed it to the parser with str_get_html()
or $parser->load()
; the class itself does pretty much the same internally with the method you're using right now.
这篇关于简单的HTML DOM解析器-发送帖子变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!