Hướng dẫn dùng accept language trong PHP

[PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8]

stream_context_createCreates a stream context

Description

stream_context_create[?array $options = null, ?array $params = null]: resource

Parameters

options

Must be an associative array of associative arrays in the format $arr['wrapper']['option'] = $value, or null. Refer to context options for a list of available wrappers and options.

Defaults to null.

params

Must be an associative array in the format $arr['parameter'] = $value, or null. Refer to context parameters for a listing of standard stream parameters.

Return Values

A stream context resource.

Changelog

VersionDescription
8.0.0 options and params are now nullable.

Examples

Example #1 Using stream_context_create[]

See Also

  • stream_context_set_option[] - Sets an option for a stream/wrapper/context
  • Listing of supported wrappers [Supported Protocols and Wrappers]
  • Context options [Context options and parameters]

jrubenstein at gmail dot com

15 years ago

Something to keep in mind when creating SSL streams [using //]:



One would think - the proper way to create a stream options array, would be as follows:



THAT IS THE WRONG WAY!!!
Take notice to the 3rd line: 'https' => array [

The CORRECT way, is as follows:



Notice, the NEW 3rd line: 'http' => array [

Now - keep this in mind - I spent several hours trying to trouble shoot my issue, when I finally stumbled upon this non-documented issue.

The complete code to post to a secure page is as follows:

contact [at] thepointsolution.com

12 years ago

I big NOTE that i hope will help some one. Something that is not mentioned in the documentation, is that when php is compiled --with-curlwrappers,

So, instead of:



You would setup the header this way:



This will work.

net_navard at yahoo dot com

16 years ago

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['//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

Ben J

9 years ago

I spent a good five hours trying to figure this out, so hopefully it will save someone else some time.

When you are trying to download a file via ftp through an HTTP proxy note that the following will not be enough:


Your proxy will respond that authentication is required. You may scratch your head and think "but I'm providing authentication!"

The issue is that the 'header' value is only applicable to http connections. So to authenticate on a proxy, you first have to pull a file from HTTP, before the context is valid for using on FTP.


It's a bit roundabout, but it works. Note that the 'header' val in the ftp array is redundant, but I kept it in anyway.

Brian Gottier

13 years ago

In some cases, set a header option as an array, and not a string, depending on server configuration.

Chủ Đề