php stream_context_create函数
stream_context_create创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程.
函数原型: resource stream_context_create ([array $options [,array $params ]] ),看个实例:
//定义options数组 $opts = array ( 'http' => array ( 'method' => "get" , 'header' => "accept-language: enrn" . "cookie: foo=barrn" ) ); //创建数据流上下文 $context =stream_context_create( $opts ); /*向指定地址发送http请求 请求中包含附加的头部信息*/ $fp = fopen ( 'http://HdhCmsTest111cn.net' , 'r' ,false, $context ); //输出文件指针处的所有数据 fpassthru ( $fp ); //关闭文件 fclose( $fp ); /* //该代码的输出结果为:即请求的cookie值 array ( [foo] => bar ) */实例二,代码如下:
$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 ); /* sends a regular get request to proxy server at 10.54.1.39 * for HdhCmsTestphpfensi测试数据 using context options specified in $default_opts */ readfile( 'http://HdhCmsTestphpfensi测试数据' ); /* sends a post request directly to HdhCmsTestphpfensi测试数据 * using context options specified in $alternate_opts */ readfile( 'http://HdhCmsTestphpfensi测试数据' , false, $alternate );查看更多关于php stream_context_create函数 - php函数的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did31014