PHPstream_context_create () function and usage analysis

Source: Internet
Author: User
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 );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.