php file_get_contents()请求页面发送cookie实例源码

时间:2016-09-01
php使用file_get_contents获取文件内容,有的时候需要为请求的页面设置cookie信息,本文章向大家介绍file_get_contents()请求页面时如何发送cookie信息,需要的朋友可以参考一下。

具体代码如下:

<?php
$opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"));
$context = stream_context_create($opts);
/*  http://www.manongjc.com/article/1430.html */
$contents = file_get_contents('http://example.com/test1.txt', false, $context);
echo $contents;
?>