简体   繁体   English

在PHP简单HTML DOM解析器中设置用户代理参数

[英]Seting User Agent param in PHP Simple HTML DOM Parser

有什么方法可以将用户代理字符串以及PHP Simple HTML DOM解析器发送的请求包括在内?

Thanks Michal Čihař, you are right I just made some changes in the load_file() function of simple_html_dom class and it worked 感谢MichalČihař,您是对的,我刚刚对simple_html_dom类的load_file()函数进行了一些更改,并且它可以正常工作

// load html from file
    function load_file() {
        $args = func_get_args();
        // Added by Mithun
        $opts = array(
            'http'=>array(
                'method'=>"GET",
                'header'=>"Accept-language: en\r\n" .
                "User-Agent:    Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6\r\n".
                "Cookie: foo=bar\r\n"
            )
        );
        $context = stream_context_create($opts);
        $args[1] = FALSE;
        $args[2] = $context;
        // End Mithun
        $this->load(call_user_func_array('file_get_contents', $args), true);
    }

By looking at the code it should be possible by using context streams, something like: 通过查看代码,应该可以使用上下文流,例如:

$context = stream_context_create();
stream_context_set_params($context, array('user_agent' => 'UserAgent/1.0'));
file_get_html('http://www.google.com/', 0, $context);

Alternatively you can also set default value in php.ini . 另外,您也可以在php.ini中设置默认值。

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

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