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:
Copy codeThe Code is as follows:
<? 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.jb51.net
With additional headers shown abve */
$ Fp = fopen ('HTTP: // www.jb51.net', 'R', false, $ context );
Fpassthru ($ fp );
Fclose ($ fp );
?>
Example 2:
Copy codeThe Code is as follows:
<? Php
$ 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:
<? Php
$ Opts = array ('HTTP --> array (
'Method' => "GET ",
'Header' => array ("Accept-language: en ",
"Cookie: foo = bar ",
"Custom-Header: value ")
)
);
$ Context = stream_context_create ($ opts );
?>
Example 3:
Copy codeThe Code is as follows:
<? Php
$ 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:
Copy codeThe Code is as follows:
<? Php
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 );
?>