stream_context_create创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程.
函数原型:resource stream_context_create ([array $options [,array $params ]] ),看个实例:
-
- $opts=array
- (
- 'http'=>array
- (
- 'method'=>"get",
- 'header'=>"accept-language: enrn"."cookie: foo=barrn"
- )
- );
-
- $context=stream_context_create($opts);
-
-
- $fp=fopen('http://www.111cn.net','r',false,$context);
-
- fpassthru($fp);
-
- fclose($fp);
-
-
-
-
-
-
-
实例二,代码如下:
- $default_opts=array
- (
- 'http'=>array
- (
- 'method'=>"get",
- 'header'=>"accept-language: enrn"."cookie: foo=bar",
- 'proxy'=>"tcp://10.54.1.39:8000"
- )
- );
- $alternate_opts=array
- (
- 'http'=>array
- (
- 'method'=>"post",
- 'header'=>"content-type: application/x-www-form-urlencodedrn"."content-length: " . strlen("baz=bomb"),
- 'content'=>"baz=bomb"
- )
- );
- $default=stream_context_get_default($default_opts);
- $alternate=stream_context_create($alternate_opts);
-
-
-
- readfile('http://www.phpfensi.com');
-
-
-
- readfile('http://www.phpfensi.com', false, $alternate);
|