我们在使用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);
?> 

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

相关文章:

  • 2021-11-16
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2022-01-12
  • 2021-07-20
  • 2022-12-23
  • 2022-02-05
猜你喜欢
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2021-10-01
  • 2021-10-10
相关资源
相似解决方案