当前位置:首页 > PHP教程 > php函数 > 列表

php stream_context_create函数

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-19 16:19:30 浏览: 评论:0 

stream_context_create创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程.

函数原型:resource stream_context_create ([array $options [,array $params ]] ),看个实例:

  1. //定义options数组 
  2. $opts=array 
  3.   'http'=>array 
  4.   ( 
  5.     'method'=>"get"
  6.     'header'=>"accept-language: enrn"."cookie: foo=barrn" 
  7.   ) 
  8. ); 
  9. //创建数据流上下文 
  10. $context=stream_context_create($opts); 
  11. /*向指定地址发送http请求 
  12. 请求中包含附加的头部信息*/ 
  13. $fp=fopen('http://www.111cn.net','r',false,$context); 
  14. //输出文件指针处的所有数据 
  15. fpassthru($fp); 
  16. //关闭文件 
  17. fclose($fp); 
  18. /* 
  19. //该代码的输出结果为:即请求的cookie值 
  20. array 
  21. ( 
  22.     [foo] => bar 
  23. ) 
  24. */ 

实例二,代码如下:

  1. $default_opts=array 
  2.   'http'=>array 
  3.   ( 
  4.     'method'=>"get"
  5.     'header'=>"accept-language: enrn"."cookie: foo=bar"
  6.     'proxy'=>"tcp://10.54.1.39:8000" 
  7.   ) 
  8. ); 
  9. $alternate_opts=array 
  10.   'http'=>array 
  11.   ( 
  12.     'method'=>"post"
  13.     'header'=>"content-type: application/x-www-form-urlencodedrn"."content-length: " . strlen("baz=bomb"), 
  14.     'content'=>"baz=bomb" 
  15.   ) 
  16. ); 
  17. $default=stream_context_get_default($default_opts); 
  18. $alternate=stream_context_create($alternate_opts); 
  19. /* sends a regular get request to proxy server at 10.54.1.39 
  20. * for www.phpfensi.com using context options specified in $default_opts 
  21. */ 
  22. readfile('http://www.phpfensi.com'); 
  23. /* sends a post request directly to www.phpfensi.com 
  24. * using context options specified in $alternate_opts 
  25. */ 
  26. readfile('http://www.phpfensi.com', false, $alternate);

Tags: stream_context_create php函数

分享到: