PHP 中的流上下文 - 它是什么? [英] Stream context in PHP - what is it?
问题描述
我已经搜索了几个小时,但我无法弄清楚 PHP 中的流上下文"是什么.我正在尝试使用 API,它涉及使用此流上下文".
I have searched for hours and I cannot figure out what a 'stream context' in PHP is. I'm trying to use an API and it involves using this 'stream context'.
文档说:
上下文是一组参数和特定于包装器的选项,用于修改或增强流的行为.
A context is a set of parameters and wrapper specific options which modify or enhance the behavior of a stream.
- 什么的参数?
- 特定于包装器"的选项是什么意思?
- 什么流?
这是我正在谈论的代码:
Here is the code I'm talking about:
// Encode the credentials and create the stream context.
$auth = base64_encode("$acctKey:$acctKey");
$data = array(
'http' => array(
'request_fulluri' => true,
// ignore_errors can help debug – remove for production. This option added in PHP 5.2.10
'ignore_errors' => true,
'header' => "Authorization: Basic $auth")
);
$context = stream_context_create($data);
// Get the response from Bing.
$response = file_get_contents($requestUri, 0, $context);
推荐答案
修改流属性的上下文参数.
A parameter of the context that modifies the properties of the stream.
这些选项特定于流使用的任何包装器.这些示例包括文件、所有不同的 php://
URI、HTTP 包装器(就像您执行 file_get_contents('http://example.com')
时那样)—它与 file_get_contents('some-file.txt')
)
The options are specific to whatever wrapper the stream is using. Examples of these include files, all the different php://
URIs, the HTTP wrapper (like when you do file_get_contents('http://example.com')
— it’s not the same thing as file_get_contents('some-file.txt')
)
任何流!
在这种情况下,流上下文被传递给 file_get_contents
以告诉它将该授权标头和那些选项发送到允许 file_get_contents
从 HTTP 获取内容的包装器网址.
In this case, the stream context is passed to file_get_contents
to tell it to send that authorization header and those options to the wrapper that allows file_get_contents
to get contents from HTTP URLs.
You can find a list of the HTTP context options on the PHP website.
这篇关于PHP 中的流上下文 - 它是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!