首页 > Web开发 > 详细

PHP为fopen,file_get_contents等函数请求web地址时增加Http头的方法

时间:2014-01-20 08:43:43      阅读:430      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
我们在使用fsockopen时可以方便的自定义自己请求的http头内容来访问某些对客户端请求头有特殊限制的网站,但是使用fopen,file_get_contents等函数请求web地址时怎么来灵活定义请求的http头呢?

解决方案:stream_context_create() 函数

fopen实现

<?php
$opts = array(
  ‘http‘=>array(
    ‘method‘=>"GET",
    ‘header‘=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);
$fp = fopen(‘http://www.example.com‘, ‘r‘, false, $context);
fpassthru($fp);
fclose($fp);
?> 
 
file_get_contents实现
 
<?php
$opts = array(
  ‘http‘=>array(
    ‘method‘=>"GET",
    ‘header‘=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

file_get_contents(‘http://www.example.com‘, null, $context);
?> 
bubuko.com,布布扣

http://be-evil.org/post-174.html

PHP为fopen,file_get_contents等函数请求web地址时增加Http头的方法

原文:http://www.cnblogs.com/andydao/p/3525993.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!