Creates and returns a text data stream and applies various options. it can be used in special procedures such as timeout setting, proxy server, request method, and header information setting for fopen () and file_get_contents. Purpose: Create and return a text data stream and apply various options for fopen (), file_get_contents () special procedures for timeout settings, proxy server, request method, and header information settings.
Function prototype: resource stream_context_create ([array $ options [, array $ params])
Usage
Example 1:
The code is as follows:
$ 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.jb51.net
With additional headers shown abve */
$ Fp = fopen ('http: // www.jb51.net', 'R', false, $ context );
Fpassthru ($ fp );
Fclose ($ fp );
?>
Example 2:
The code is as follows:
$ Opts = array ('http --> array (
'Method' => "GET ",
'Header' => "Accept-language: en \ r \ n ".
"Cookie: foo = bar \ r \ n"
)
);
$ Context = stream_context_create ($ opts );
?>
You wocould setup the header this way:
$ Opts = array ('http --> array (
'Method' => "GET ",
'Header' => array ("Accept-language: en ",
"Cookie: foo = bar ",
"Custom-Header: value ")
)
);
$ Context = stream_context_create ($ opts );
?>
Example 3:
The code is as follows:
$ Opts = array ('http' => array ('proxy' => 'tcp: // 127.0.0.1: 8080 ', 'request _ fulluri' => true ));
$ Context = stream_context_create ($ opts );
$ Data = file_get_contents ('http: // www.jb51.net', false, $ context );
Echo $ data;
?>
Example 4:
The code is as follows:
Function do_post_request ($ url, $ postdata, $ files = null)
{
$ Data = "";
$ Boundary = "-------------------". substr (md5 (rand (), 0, 10 );
// Collect Postdata
Foreach ($ postdata as $ key => $ val)
{
$ Data. = "-- $ boundary \ n ";
$ Data. = "Content-Disposition: form-data; name = \" ". $ key." \ "\ n". $ val. "\ n ";
}
$ Data. = "-- $ boundary \ n ";
// Collect Filedata
Foreach ($ files as $ key => $ file)
{
$ FileContents = file_get_contents ($ file ['tmp _ name']);
$ Data. = "Content-Disposition: form-data; name = \" {$ key} \ "; filename = \" {$ file ['name']} \ "\ n ";
$ Data. = "Content-Type: image/jpeg \ n ";
$ Data. = "Content-Transfer-Encoding: binary \ n ";
$ Data. = $ fileContents. "\ n ";
$ Data. = "-- $ boundary -- \ n ";
}
$ Params = array ('http' => array (
'Method' => 'post ',
'Header' => 'Content-Type: multipart/form-data; boundary = '. $ boundary,
'Content' => $ data
));
$ Ctx = stream_context_create ($ params );
$ Fp = fopen ($ url, 'RB', false, $ ctx );
If (! $ Fp ){
Throw new Exception ("Problem with $ url, $ php_errormsg ");
}
$ Response = @ stream_get_contents ($ fp );
If ($ response = false ){
Throw new Exception ("Problem reading data from $ url, $ php_errormsg ");
}
Return $ response;
}
// Set data (in this example from post)
// Sample data
$ Postdata = array (
'Name' => $ _ POST ['name'],
'Age' => $ _ POST ['age'],
'Sex '=> $ _ POST ['Sex']
);
// Sample image
$ Files ['image'] = $ _ FILES ['image'];
Do_post_request ("http://www.jb51.net", $ postdata, $ files );
?>