stream_context_create創(chuàng)建并返回一個文本數據流并應用各種選項,可用于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"
- )
- );
- //創(chuàng)建數據流上下文
- $context=stream_context_create($opts);
- /*向指定地址發(fā)送http請求
- 請求中包含附加的頭部信息*/
- $fp=fopen('http://www.111cn.net','r',false,$context);
- //輸出文件指針處的所有數據
- fpassthru($fp);
- //關閉文件
- fclose($fp);
- /*
- //該代碼的輸出結果為:即請求的cookie值
- array
- (
- [foo] => bar
- )
- */
實例二,代碼如下:
轉載請注明來源:php stream_context_create函數
- $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 www.phpfensi.com using context options specified in $default_opts
- */
- readfile('http://www.phpfensi.com');
- /* sends a post request directly to www.phpfensi.com
- * using context options specified in $alternate_opts
- */
- readfile('http://www.phpfensi.com', false, $alternate);
哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網站,并且能夠搭建符合百度排名規(guī)范的網站基底,使您的網站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網站建設。
