Seting User Agent param in PHP Simple HTML DOM Parser

非 Y 不嫁゛ 提交于 2019-12-03 06:25:58

问题


Is there any way to include the user agent string along with the request send by PHP Simple HTML DOM Parser?


回答1:


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.




回答2:


Thanks Michal Čihař, you are right I just made some changes in the load_file() function of simple_html_dom class and it worked

// 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);
    }


来源:https://stackoverflow.com/questions/1945511/seting-user-agent-param-in-php-simple-html-dom-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!