Php stream_context_create function

Source: Internet
Author: User

Function prototype: resource stream_context_create ([array $ options [, array $ params])

View instances

// Define the options array
$ Opts = array
(
'Http' => array
  (
'Method' => "get ",
'Header' => "accept-language: enrn". "cookie: foo = barrn"
  )
);
// Create a data stream context
$ Context = stream_context_create ($ opts );
/* Send an http request to the specified address
The request contains the additional header information */
$ Fp = fopen ('http: // www.111cn.net ', 'R', false, $ context );
// Output all data at the file pointer
Fpassthru ($ fp );
// Close the file
Fclose ($ fp );
/*
The output result of this code is: cookie value of the request
Array
(
[Foo] => bar
)
*/

Instance 2

$ Default_opts = array
(
'Http' => array
  (
'Method' => "get ",
'Header' => "accept-language: enrn". "cookie: foo = bar ",
'Proxy' => "tcp: // 10.54.1.39: 8000"
  )
);
$ Alternate_opts = array
(
'Http' => array
  (
'Method' => "post ",
'Header' => "content-type: application/x-www-form-urlencodedrn". "content-length:". strlen ("baz = bomb "),
'Content' => "baz = bomb"
  )
);
$ Default = stream_context_get_default ($ default_opts );
$ Alternate = stream_context_create ($ alternate_opts );
/* Sends a regular get request to proxy server at 10.54.1.39
* For www.example.com using context options specified in $ default_opts
*/
Readfile ('http: // www.example.com ');
/* Sends a post request directly to www.example.com
* Using context options specified in $ alternate_opts
*/
Readfile ('http: // www.example.com ', false, $ alternate );

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.