PHP中file_get_contents如何带上cookies

来源:互联网 发布:中亚五斯坦知乎 编辑:程序博客网 时间:2024/05/01 17:17
$opts = array (     'http' => array (         'method' => 'GET',         'header'=>          "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" .         "Cookie:a=as; \r\n"."Pragma:no-cache\r\n",         ));  $context = stream_context_create($opts);$result_data=file_get_contents($url,false,$context);

以上代码,实现了构造http请求头去访问url

能够构造http请求头,可以干很多方便的事情

  1. url中采用IP访问,而在请求头中,指定host的值,这样的话,就不用系统绑host操作了。

  2. 可以灵活构造Cookies

  3. 请求头标明不支持gzip等格式返回,避免对方服务器以gzip格式返回,需解析内容才能抓取

0 0
原创粉丝点击