5

我有用于 PHP 的简单 HTML DOM 解析器,我正在使用以下标记:

$html = file_get_html('http://www.google.com');

但是,如何将发布变量(如 cURL)发送到该页面并获得响应?例如

$html = file_get_html('http://www.google.com', array("Item"=>"Value", "Item2"=>"Value2"));
4

1 回答 1

11

据我所知,文档没有提到它,但是在查看源代码后,我注意到您正在使用的函数接受流上下文作为其第三个参数。您可以使用此 PHP 功能创建一个发布请求,如下所示:

$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()or将其提供给解析器$parser->load();该类本身在内部与您现在使用的方法几乎相同。

于 2012-02-29T10:24:37.007 回答