【问题标题】:Simple HTML DOM Parser - Send post variables简单的 HTML DOM 解析器 - 发送帖子变量
【发布时间】:2012-03-18 19:44:48
【问题描述】:

我有用于 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"));

【问题讨论】:

    标签: php domparser


    【解决方案1】:

    据我所知,文档没有提到它,但在查看源代码后,我注意到您使用的函数接受 stream context 作为其第三个参数。您可以使用这个 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()$parser->load() 将其提供给解析器;该类本身在内部与您现在使用的方法几乎相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 2015-05-17
      • 2015-02-14
      相关资源
      最近更新 更多