stream_context_create創(chuàng)建并返回一個文本數據流并應用各種選項,可用于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. //創(chuàng)建數據流上下文 
  10. $context=stream_context_create($opts); 
  11. /*向指定地址發(fā)送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);
轉載請注明來源:php stream_context_create函數

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網站,并且能夠搭建符合百度排名規(guī)范的網站基底,使您的網站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網站建設