stream_context_create

(PHP 4 >= 4.3.0, PHP 5)

stream_context_create -- Create a streams context

Description

resource stream_context_create ( [array options] )

Creates and returns a stream context with any options supplied in options preset.

options must be an associative array of associative arrays in the format $arr['wrapper']['option'] = $value. It defaults to an empty array.

例子 1. Using stream_context_create()

<?php
$opts
= array(
  
'http'=>array(
    
'method'=>"GET",
    
'header'=>"Accept-language: en\r\n" .
              
"Cookie: foo=bar\r\n"
  
)
);

$context = stream_context_create($opts);

/* Sends an http request to www.example.com
   with additional headers shown above */
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>

See also stream_context_set_option(), and Listing of supported wrappers with context options (附录 L).


add a note add a note User Contributed Notes
dev at zayso dot org
06-Mar-2006 09:31
Example of a stream for reading a string passed
via a context object.
<?php
/* ----------------------------------------
 * Designed to read from a string
 */
class sfStreamStringRead
{
   const
PROTOCOL = 'stringread'; /* Underscore not allowed */
      
  
protected $dataPos  = NULL;
  
protected $dataBuf  = NULL;
  
protected $dataLen  = NULL;
  
   function
stream_open($path, $mode, $options, &$opened_path)
   {
      
/* Verify context has data */
      
$contextOptions = stream_context_get_options($this->context);
       if (!isset(
$contextOptions[self::PROTOCOL]['data'])) {
           return
FALSE;
       }
      
$this->dataBuf = $contextOptions[self::PROTOCOL]['data'];
      
$this->dataLen = strlen($this->dataBuf);
      
$this->dataPos = 0;
       return
TRUE;
   }
   function
stream_read($count){
      
$ret = substr($this->dataBuf, $this->dataPos, $count);
      
$this->dataPos += strlen($ret);
       return
$ret;
   }
   function
stream_eof(){
       return
$this->dataPos >= $this->dataLen;
   }
   function
stream_tell(){
       return
$this->dataPos;
   }
  
/* ------------------------------------------
     * A few helper functions
     */
  
static function genURL()
   {
       return
self::PROTOCOL . '://';
   }
   static function
genContext($dataBuf)
   {
       return
stream_context_create(array(
          
self::PROTOCOL => array(
              
'data' => $dataBuf,
           ),
       ));
   }
   static function
open($dataBuf)
   {
       return
fopen(self::genURL(),'r',FALSE,self::genContext($dataBuf));
   } 
}
stream_wrapper_register(
  
sfStreamStringRead::PROTOCOL,
  
'sfStreamStringRead'
);

$sp = sfStreamStringRead::open("Some String Data\n");
echo
fgets($sp);
fclose($sp);       

?>
net_navard at yahoo dot com
10-Dec-2005 02:38
Hi,you can create an array of parameters(what it's called a stream context),which can be transmitted each time you read or write a stream through a socket.In the below example:

$opts =array('http'=>arra('method'=>"GET",
'header'=>"Accept-language:en\r\n"."Cookie: foo=bar\r\n");

What you're actually doing is create a set of parameters(the protocol to be used,the request method,additional http headers and a cookie) which will be used each time you open a socket connection to request www.example.com.This saves a lot of time if you want to use these parameters (called a stream context) whenever you include them when making a request to www.example.com,instead of having to specify them over and over again.
Using the previous example,say you want to create a stream context,which sends a "Content-Type" http header and utilize it when making a request to www.example.com.Take a look:

$opts = array('http'=>array('method'=>"GET",
'header'=>"Content-Type: text/xml; charset=utf-8");

$context = stream_context_create($opts);
$fp = fopen('http://www.example.com','r',false,$context);
fpassthru($fp);
fclose($fp);

Now,when you make a request to www.example.com,the above http header will be included within the socket and transmitted to the server.Best of luck for you friends,Hossein