ΠžΠΏΡ†ΠΈΠΈ HTTP-контСкста

ΠžΠΏΡ†ΠΈΠΈ HTTP-контСкста β€” Бписок ΠΎΠΏΡ†ΠΈΠΉ HTTP-контСкста

ОписаниС

ΠžΠΏΡ†ΠΈΠΈ контСкста для транспортных ΠΏΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ»ΠΎΠ² http:// ΠΈ https://.

ΠžΠΏΡ†ΠΈΠΈ

method string

ΠœΠ΅Ρ‚ΠΎΠ΄Ρ‹ GET, POST ΠΈΠ»ΠΈ любой Π΄Ρ€ΡƒΠ³ΠΎΠΉ HTTP-ΠΌΠ΅Ρ‚ΠΎΠ΄, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ ΡƒΠ΄Π°Π»Ρ‘Π½Π½Ρ‹ΠΉ сСрвСр.

По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ β€” GET.

header array ΠΈΠ»ΠΈ string

Π”ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ для ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ Π² запросС. ЗначСния Π² этой ΠΎΠΏΡ†ΠΈΠΈ ΠΏΠ΅Ρ€Π΅ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΡΡŽΡ‚ Π΄Ρ€ΡƒΠ³ΠΈΠ΅ значСния Π½Π°ΠΏΠΎΠ΄ΠΎΠ±ΠΈΠ΅ User-Agent:, Host: ΠΈ Authentication:, Π΄Π°ΠΆΠ΅ ΠΏΡ€ΠΈ слСдовании пСрСнаправлСниям Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ° Location:. ΠŸΠΎΡΡ‚ΠΎΠΌΡƒ Π»ΡƒΡ‡ΡˆΠ΅ Π½Π΅ ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°Ρ‚ΡŒ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ Host:, Ссли Π²ΠΊΠ»ΡŽΡ‡ΠΈΠ»ΠΈ ΠΎΠΏΡ†ΠΈΡŽ follow_location.

Π‘Ρ‚Ρ€ΠΎΠΊΠΎΠ²Ρ‹Π΅ значСния ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ ΠΊΠ°ΠΊ ΠΏΠ°Ρ€Ρ‹ ΠšΠ»ΡŽΡ‡: Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΈ Ρ€Π°Π·Π΄Π΅Π»ΡΡŽΡ‚ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒΡŽ символов \r\n, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€: "Content-Type: application/json\r\nConnection: close". Π’ массивС значСния ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ ΠΊΠ°ΠΊ список ΠΏΠ°Ρ€ ΠšΠ»ΡŽΡ‡: Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€: ["Content-Type: application/json", "Connection: close"].

user_agent string

Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ для ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ Π² Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ΅ User-Agent:. Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ учитываСтся, Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ссли Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ User-Agent Π½Π΅ ΡƒΠΊΠ°Π·Π°Π»ΠΈ Π² ΠΎΠΏΡ†ΠΈΠΈ контСкста header Π²Ρ‹ΡˆΠ΅.

По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ извлСкаСтся Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹ user_agent ΠΈΠ· Ρ„Π°ΠΉΠ»Π° php.ini.

content string

Π”ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ для ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ послС Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΎΠ². ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ часто ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ с POST- ΠΈ PUT-запросами.

proxy string

URI-ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚ адрСс прокси-сСрвСра, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€: tcp://proxy.example.com:5100.

request_fulluri bool

Π‘ΠΎ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ΠΌ true ΠΏΡ€ΠΈ Ρ„ΠΎΡ€ΠΌΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠΈ запроса Π·Π°ΠΏΡ€Π°ΡˆΠΈΠ²Π°Π΅Ρ‚ΡΡ вСсь URI, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€: GET http://www.example.com/path/to/file.html HTTP/1.0. НСкоторыС прокси-сСрвСры Ρ‚Ρ€Π΅Π±ΡƒΡŽΡ‚ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Ρ‚ΡŒ вСсь ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€, хотя это нСстандартный Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ запроса.

По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ false.

follow_location int

ΠžΠΏΡ†ΠΈΡ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚, ΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚ΡŒ Π»ΠΈ пСрСнаправлСниям Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ° Location. Π§Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒ, ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°ΡŽΡ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 0.

По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ β€” 1.

max_redirects int

МаксимальноС количСство ΠΏΠ΅Ρ€Π΅Π½Π°ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠΉ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ ΠΌΠΎΠΆΠ½ΠΎ ΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚ΡŒ. Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 1 ΠΈΠ»ΠΈ мСньшС ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ пСрСнаправлСния Π½Π΅ Π²Ρ‹ΠΏΠΎΠ»Π½ΡΡŽΡ‚ΡΡ.

По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ 20.

protocol_version float

ВСрсия HTTP-ΠΏΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ»Π°.

Π‘ PHP 8.0.0 Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ β€” 1.1; Π΄ΠΎ этой вСрсии Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ Ρ€Π°Π²Π½ΡΠ»ΠΎΡΡŒ 1.0.

timeout float

ВрСмя оТидания Π½Π° Ρ‡Ρ‚Π΅Π½ΠΈΠ΅ Π² сСкундах Π² Π²ΠΈΠ΄Π΅ числа с ΠΏΠ»Π°Π²Π°ΡŽΡ‰Π΅ΠΉ Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ (float), Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€: 10.5.

По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ извлСкаСтся Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹ default_socket_timeout ΠΈΠ· Ρ„Π°ΠΉΠ»Π° php.ini.

ignore_errors bool

ΠžΠΏΡ†ΠΈΡ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚, ΠΈΠ·Π²Π»Π΅ΠΊΠ°Ρ‚ΡŒ Π»ΠΈ содСрТимоС Π΄Π°ΠΆΠ΅ ΠΏΡ€ΠΈ Π½Π΅ΡƒΡΠΏΠ΅ΡˆΠ½Ρ‹Ρ… статусах Π·Π°Π²Π΅Ρ€ΡˆΠ΅Π½ΠΈΡ.

По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ β€” false.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ получСния страницы ΠΈ ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ Π΄Π°Π½Π½Ρ‹Ρ… ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠΌ POST

<?php

$postdata
= http_build_query(
[
'var1' => 'Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ содСрТимоС',
'var2' => 'doh',
]
);

$opts = [
'http' => [
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata,
]
];

$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ игнорирования ΠΏΠ΅Ρ€Π΅Π½Π°ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠΉ, Π½ΠΎ с ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ΠΌ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΎΠ² ΠΈ ΠΊΠΎΠ½Ρ‚Π΅Π½Ρ‚Π°

<?php

$url
= "http://www.example.org/header.php";

$opts = [
'http' => [
'method' => 'GET',
'max_redirects' => '0',
'ignore_errors' => '1',
]
];

$context = stream_context_create($opts);
$stream = fopen($url, 'r', false, $context);

// Π˜Π½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡ ΠΎ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ°Ρ…
// ΠΈ ΠΌΠ΅Ρ‚Π°Π΄Π°Π½Π½Ρ‹Π΅ ΠΎ ΠΏΠΎΡ‚ΠΎΠΊΠ΅
var_dump(stream_get_meta_data($stream));

// ЀактичСскиС Π΄Π°Π½Π½Ρ‹Π΅ Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $url
var_dump(stream_get_contents($stream));
fclose($stream);

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: ΠžΠΏΡ†ΠΈΠΈ контСкста ΠΏΠΎΡ‚ΠΎΠΊΠ° Π½ΠΈΠΆΠ΅Π»Π΅ΠΆΠ°Ρ‰Π΅Π³ΠΎ сокСта
НиТСлСТащий транспортный ΠΏΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ» ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Ρ‚ΡŒ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ ΠΎΠΏΡ†ΠΈΠΈ контСкста. Для ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ² http:// смотритС описаниС ΠΎΠΏΡ†ΠΈΠΉ контСкста для транспортного ΠΏΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ»Π° tcp://. Для ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ² https:// смотритС описаниС ΠΎΠΏΡ†ΠΈΠΉ контСкста для транспортного ΠΏΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ»Π° ssl://.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Π‘Ρ‚Ρ€ΠΎΠΊΠ° HTTP-статуса
Когда эта ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΠ° ΠΏΠΎΡ‚ΠΎΠΊΠ° слСдуСт пСрСнаправлСниям, элСмСнт wrapper_data, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ функция stream_get_meta_data(), Π½Π΅ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ содСрТит строку HTTP-статуса, которая Π½Π° самом Π΄Π΅Π»Π΅ относится ΠΊ ΡΠΎΠ΄Π΅Ρ€ΠΆΠ°Π½ΠΈΡŽ Π΄Π°Π½Π½Ρ‹Ρ… Π² индСксС 0.

array (
  'wrapper_data' =>
  array (
    0 => 'HTTP/1.0 301 Moved Permanently',
    1 => 'Cache-Control: no-cache',
    2 => 'Connection: close',
    3 => 'Location: http://example.com/foo.jpg',
    4 => 'HTTP/1.1 200 OK',
    ...
ΠŸΠ΅Ρ€Π²Ρ‹ΠΉ запрос Π²Π΅Ρ€Π½ΡƒΠ» ΠΊΠΎΠ΄ 301 (постоянноС ΠΏΠ΅Ρ€Π΅Π½Π°ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅), поэтому ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΠ° ΠΏΠΎΡ‚ΠΎΠΊΠ° автоматичСски послСдовала ΠΏΠ΅Ρ€Π΅Π½Π°ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΡŽ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ ΠΎΡ‚Π²Π΅Ρ‚ с ΠΊΠΎΠ΄ΠΎΠΌ 200 (индСкс = 4).
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 12 notes

up
33
nate ΒΆ
12 years ago
Note that if you set the protocol_version option to 1.1 and the server you are requesting from is configured to use keep-alive connections, the function (fopen, file_get_contents, etc.) will "be slow" and take a long time to complete. This is a feature of the HTTP 1.1 protocol you are unlikely to use with stream contexts in PHP.

Simply add a "Connection: close" header to the request to eliminate the keep-alive timeout:

<?php
// php 5.4 : array syntax and header option with array value
$data = file_get_contents('http://www.example.com/', null, stream_context_create([
    'http' => [
        'protocol_version' => 1.1,
        'header'           => [
            'Connection: close',
        ],
    ],
]));
?>
up
17
daniel dot peder at gmail dot com ΒΆ
8 years ago
note that for both http and https protocols require the same 'http' context keyword:

<?php

// CORRECT example:
// this will work as expected
// note the url with https but context with http
$correct_data = file_get_contents('https://example.com', false, stream_context_create(array('http' => array(...))));

// INVALID example:
// this will not work, the context will be ignored
// note the url with https also context with https
$correct_data = file_get_contents('https://example.com', false, stream_context_create(array('https' => array(...))));
up
6
ASchmidt at Anamera dot net ΒΆ
5 years ago
With the default of 

    'follow_location'   =>  1

be certain NEVER include a "Host:" header in the 'header' array.

If the host is set to "mydomain.com", and that web site has a (quite common) redirect to "www.mydomain.com", then the initial request to "http://mydomain.com" will get the expected response of:

HTTP/1.1 301 Moved Permanently
Location: http://www.mydomain.com/

However, the follow-up request does NOT replace the original "host" header with the new "location" value, as one would expect. Consequently each "follow-location" request will again be served by the original host of "http://mydomain.com", and continue the redirect loop until 'max_redirects' has been exhausted.

(For details: https://bugs.php.net/bug.php?id=77889)
up
17
daniel.peder (a) gmail.com ΒΆ
8 years ago
note that both http and https transports require the same context name http

// OK example:
// this will work as expected
// note the url with https but context with http
$correct_data = file_get_contents('https://example.com', false, stream_context_create(array('http' => array(...))));

// INVALID example:
// this will not work, the context will be ignored
// note the url with https also context with https
$correct_data = file_get_contents('https://example.com', false, stream_context_create(array('https' => array(...))));
up
17
gourav sarkar ΒΆ
15 years ago
watch your case when using methods (POST and GET)...it must be always uppercase. in case of you write it in lower case it wont work.
up
5
ywarnier at beeznest dot org ΒΆ
8 years ago
Note that setting request_fulluri to true will *change* the value of $_SERVER['REQUEST_URI] on the receiving end (from /abc.php to http://domain.com/abc.php).
up
7
aruntechguy at outlook dot com ΒΆ
8 years ago
If you want to use Basic Auth while using get_headers(), use stream_context options below. 

I am using HEAD method here, but please feel free to use GET also.

<?php
$targetUrl = 'http or https target url here';
$basicAuth = base64_encode('username:password');

stream_context_set_default(
    [
        'http' => [
            'method' => 'HEAD',
            'header' => 'Authorization: Basic ' . $basicAuth
        ]
    ]
);
$result = get_headers($targetUrl);

print_r($result);
up
12
vchampion at gmail dot com ΒΆ
13 years ago
If you use the proxy server and encounter an error "fopen(http://example.com): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request" note that in many situations you need also set the parameter "request_fulluri" to "true" in your stream options. Without this option the php script sends the empty request to the server as "GET / HTTP/0.0" and the proxy server replies to it with the "HTTP 400" error. 

For example (working sample): 
<?php
$stream = stream_context_create(Array("http" => Array("method"  => "GET",
                                                      "timeout" => 20,
                                                      "header"  => "User-agent: Myagent",
                                                      "proxy"   => "tcp://my-proxy.localnet:3128",
                                                      'request_fulluri' => True /* without this option we get an HTTP error! */
                                )));

if ( $fp = fopen("http://example.com", 'r', false, $stream) ) {
    print "well done";
}
?>

P>S> PHP 5.3.17
up
2
njt1982 at yahoo dot com ΒΆ
1 year ago
It's worth noting that the `header` array seems to only want an array of strings, not an associative array.

I just spent a chunk of time debugging something not working as expected (no errors though) which was fixed by converting an associative array of headers into a simple array of strings.
up
7
jay ΒΆ
11 years ago
Remember to match content with Content-type:

<?php

$data = array(
    'var1' => 'some content',
    'var2' => 'doh'
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/json',  // here...
        'content' => json_encode($data)  // and here.
    )
);

. . .

?>
up
4
chris ΒΆ
12 years ago
I had quite a bit of trouble trying to make a request with fopen through a proxy to a secure url.  I kept getting a 400 Bad Request back from the remote host.  It was receiving the proxy url as the SNI host.  In order to get around this I had to explicity set the SNI host to the domain I was trying to reach.  It's apparently the issue outlined in this bug: 

https://bugs.php.net/bug.php?id=63519

<?php
$domain = parse_url($file, PHP_URL_HOST);
$proxy_string = "tcp://" . WP_PROXY_HOST  . ":" . WP_PROXY_PORT;
$opts = array( 
    'http' => array( 'proxy' => $proxy_string ),
    'ssl' => array( 'SNI_enabled' => true, 'SNI_server_name' => $domain));
$context = stream_context_create($opts);
$handle = fopen( $file, 'r', false, $context );
?>
up
1
bigtree at 29a dot nl ΒΆ
1 year ago
If you pass the 'header' option as a string and you have multiple headers, they must be separated by "\r\n".