CakePHP
  • 文档
    • 手册
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识 & 商标
  • 商业解决方案
  • 周边商品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与进来
    • 问题 (Github)
    • Bakery
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 时事通讯
    • 领英
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助 & 支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
      • Client
      • Cookie
      • Exception
      • Middleware
      • Session
      • TestSuite
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

类 Client

执行 HTTP 请求的最终用户界面。

范围化的客户端

如果你对同一个主机名进行多次请求,使用构造函数参数创建范围化的客户端通常很方便。这可以让你保持代码简洁,避免重复主机名、身份验证和其他选项。

进行请求

创建 Client 实例后,你可以使用多种方法进行请求。每种方法对应不同的 HTTP 方法。

  • get()
  • post()
  • put()
  • delete()
  • patch()

Cookie 管理

Client 会维护使用 Client 实例执行的响应中的 Cookie。这些 Cookie 会自动添加到未来对匹配主机的请求中。Cookie 会尊重 `Expires`、`Path` 和 `Domain` 属性。你可以使用 cookies() 获取 Client 的 CookieCollection。

你可以使用 `cookieJar` 构造函数选项来提供从缓存/磁盘恢复的自定义 cookie jar 实例。默认情况下,会创建一个空的 {@link \Cake\Http\Client\CookieCollection} 实例。

发送请求主体

默认情况下,任何带有 $data 的 POST/PUT/PATCH/DELETE 请求都会将其数据发送为 `application/x-www-form-urlencoded`,除非存在附加文件。在这种情况下,将使用 `multipart/form-data`。

发送请求主体时,可以使用 `type` 选项为请求设置 Content-Type。

$http->get('/users', [], ['type' => 'json']);

`type` 选项将 `Content-Type` 和 `Accept` 标头都设置为相同的 MIME 类型。使用 `type` 时,可以使用完整的 MIME 类型或别名。如果在 Accept 和 Content-Type 标头中需要不同的类型,则应手动设置它们,不要使用 `type`。

使用身份验证

使用 `auth` 键可以进行身份验证。type 子选项可用于指定要使用的身份验证策略。CakePHP 附带了一些内置策略

  • 基本
  • 摘要
  • Oauth

使用代理

使用 `proxy` 键可以为代理设置身份验证凭据(如果你需要使用代理)。type 子选项可用于指定要使用的身份验证策略。CakePHP 内置支持基本身份验证。

命名空间: Cake\Http

属性摘要

  • $_adapter protected
    Cake\Http\Client\AdapterInterface

    用于发送请求的适配器。

  • $_config protected
    array<string, mixed>

    运行时配置

  • $_configInitialized protected
    bool

    配置属性是否已使用默认值进行配置

  • $_cookies protected
    Cake\Http\Cookie\CookieCollection

    使用此客户端执行的响应中的 Cookie 列表。

  • $_defaultConfig protected
    array<string, mixed>

    客户端的默认配置。

  • $_eventClass protected
    string

    新事件对象的默认类名。

  • $_eventManager protected
    Cake\Event\EventManagerInterface|null

    此对象用来分发内部事件的 Cake\Event\EventManager 实例。

  • $_mockAdapter protected static
    Cake\Http\Client\Adapter\Mock|null

    用于在测试中存根请求的模拟适配器。

方法摘要

  • __construct() public

    创建一个新的 HTTP 客户端。

  • _addAuthentication() protected

    向请求添加身份验证标头。

  • _addProxy() protected

    添加代理身份验证标头。

  • _configDelete() protected

    删除单个配置键。

  • _configRead() protected

    读取配置键。

  • _configWrite() protected

    写入配置键。

  • _createAuth() protected

    创建身份验证策略。

  • _createRequest() protected

    根据参数创建新的请求对象。

  • _doRequest() protected

    执行非 GET 请求的辅助方法。

  • _mergeOptions() protected

    对参数与范围配置进行递归合并。

  • _sendRequest() protected

    发送请求,不进行重定向。

  • _typeHeaders() protected

    根据简短类型或完整 MIME 类型返回 Accept/Content-Type 标头。

  • addCookie() public

    向 Client 集合添加 Cookie。

  • addMockResponse() public static

    添加模拟响应。

  • buildUrl() public

    根据范围化的客户端选项生成 URL。

  • clearMockResponses() public static

    清除所有模拟响应

  • configShallow() public

    将提供的配置与现有配置合并。与 `config()` 递归合并嵌套键不同,此方法进行简单合并。

  • cookies() public

    获取 Client 中存储的 Cookie。

  • createFromUrl() public static

    返回的 Client 实例的范围限定于从传入的 URL 字符串解析的域、端口和方案。传入的字符串必须包含方案和域。可选地,如果字符串中包含端口,则端口也会被范围限定。如果 URL 中包含路径,则 Client 实例将在其前面添加路径构建 URL。URL 字符串的其他部分将被忽略。

  • delete() public

    执行 DELETE 请求。

  • dispatchEvent() public

    创建和分发事件的包装器。

  • get() public

    执行 GET 请求。

  • getConfig() public

    返回配置。

  • getConfigOrFail() public

    返回此特定键的配置。

  • getEventManager() public

    返回此对象的 Cake\Event\EventManager 管理器实例。

  • head() public

    执行 HEAD 请求。

  • options() public

    执行 OPTIONS 请求。

  • patch() public

    执行 PATCH 请求。

  • post() public

    执行 POST 请求。

  • put() public

    执行 PUT 请求。

  • send() public

    发送请求。

  • sendRequest() public

    发送 PSR-7 请求并返回 PSR-7 响应。

  • setConfig() public

    设置配置。

  • setEventManager() public

    返回此对象的 Cake\Event\EventManagerInterface 实例。

  • trace() public

    执行 TRACE 请求。

方法详情

__construct() ¶ public

__construct(array<string, mixed> $config = [])

创建一个新的 HTTP 客户端。

配置选项

创建客户端时,可以设置以下选项

  • host - 要执行请求的主机名。
  • port - 要使用的端口。
  • scheme - 要使用的默认方案/协议。默认为 http。
  • basePath - 要附加到域名的路径。(/api/v1/)
  • timeout - 超时时间(秒)。默认为 30
  • ssl_verify_peer - 是否应验证 SSL 证书。默认为 true。
  • ssl_verify_peer_name - 是否应验证对等方名称。默认为 true。
  • ssl_verify_depth - 要遍历的最大证书链深度。默认为 5。
  • ssl_verify_host - 验证证书和主机名是否匹配。默认为 true。
  • redirect - 要跟踪的重定向次数。默认为 false。
  • adapter - 适配器类名或实例。如果加载了 `curl` 扩展,则默认为 \Cake\Http\Client\Adapter\Curl,否则为 \Cake\Http\Client\Adapter\Stream。
  • protocolVersion - 要使用的 HTTP 协议版本。默认为 1.1
  • auth - 要使用的身份验证凭据。如果提供 `username` 和 `password` 键,但没有 `type` 键,则假定为基本身份验证。你可以使用 `type` 键来定义要使用的身份验证适配器类名。简短类名将解析为 `Http\Client\Auth` 命名空间。
参数
array<string, mixed> $config optional

范围化客户端的配置选项。

_addAuthentication() ¶ protected

_addAuthentication(Cake\Http\Client\Request $request, array<string, mixed> $options): Cake\Http\Client\Request

向请求添加身份验证标头。

使用身份验证类型选择正确的策略并使用其方法添加标头。

参数
Cake\Http\Client\Request $request

要修改的请求。

array<string, mixed> $options

包含“auth”键的选项数组。

返回值
Cake\Http\Client\Request

_addProxy() ¶ protected

_addProxy(Cake\Http\Client\Request $request, array<string, mixed> $options): Cake\Http\Client\Request

添加代理身份验证标头。

使用身份验证类型选择正确的策略并使用其方法添加标头。

参数
Cake\Http\Client\Request $request

要修改的请求。

array<string, mixed> $options

包含“proxy”键的选项数组。

返回值
Cake\Http\Client\Request

_configDelete() ¶ protected

_configDelete(string $key): void

删除单个配置键。

参数
string $key

要删除的键。

返回值
void
抛出
Cake\Core\Exception\CakeException
如果尝试覆盖现有配置

_configRead() ¶ protected

_configRead(string|null $key): mixed

读取配置键。

参数
string|null $key

要读取的键。

返回值
mixed

_configWrite() ¶ protected

_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void

写入配置键。

参数
array<string, mixed>|string $key

要写入的键。

mixed $value

要写入的值。

string|bool $merge optional

True 表示递归合并,'shallow' 表示简单合并,false 表示覆盖,默认值为 false。

返回值
void
抛出
Cake\Core\Exception\CakeException
如果尝试覆盖现有配置

_createAuth() ¶ protected

_createAuth(array $auth, array<string, mixed> $options): object

创建身份验证策略。

使用配置选项创建正确的身份验证策略处理程序。

参数
array $auth

要使用的身份验证选项。

array<string, mixed> $options

要使用的总体请求选项。

返回值
object
抛出
Cake\Core\Exception\CakeException
当选择无效策略时。

_createRequest() ¶ protected

_createRequest(string $method, string $url, mixed $data, array<string, mixed> $options): Cake\Http\Client\Request

根据参数创建新的请求对象。

参数
string $method

HTTP 方法名称。

string $url

包括查询字符串的 URL。

mixed $data

请求主体。

array<string, mixed> $options

要使用的选项。包含 auth、proxy 等。

返回值
Cake\Http\Client\Request

_doRequest() ¶ protected

_doRequest(string $method, string $url, mixed $data, array<string, mixed> $options): Cake\Http\Client\Response

执行非 GET 请求的辅助方法。

参数
string $method

HTTP 方法。

string $url

要请求的 URL。

mixed $data

请求主体。

array<string, mixed> $options

要使用的选项。包含 auth、proxy 等。

返回值
Cake\Http\Client\Response

_mergeOptions() ¶ protected

_mergeOptions(array<string, mixed> $options): array

对参数与范围配置进行递归合并。

参数
array<string, mixed> $options

要合并的选项。

返回值
array

_sendRequest() ¶ protected

_sendRequest(Psr\Http\Message\RequestInterface $request, array<string, mixed> $options): Cake\Http\Client\Response

发送请求,不进行重定向。

参数
Psr\Http\Message\RequestInterface $request

要发送的请求。

array<string, mixed> $options

要使用的其他选项。

返回值
Cake\Http\Client\Response

_typeHeaders() ¶ protected

_typeHeaders(string $type): array<string, string>

根据简短类型或完整 MIME 类型返回 Accept/Content-Type 标头。

参数
string $type

简短的类型别名或完整的 mimetype。

返回值
array<string, string>
抛出
Cake\Core\Exception\CakeException
当使用未知的类型别名时。

addCookie() ¶ public

addCookie(Cake\Http\Cookie\CookieInterface $cookie): $this

向 Client 集合添加 Cookie。

参数
Cake\Http\Cookie\CookieInterface $cookie

Cookie 对象。

返回值
$this
抛出
InvalidArgumentException

addMockResponse() ¶ public static

addMockResponse(string $method, string $url, Cake\Http\Client\Response $response, array<string, mixed> $options = []): void

添加模拟响应。

模拟响应存储在在调用网络适配器之前调用的适配器中。

匹配请求

TODO 完成此项。

选项

  • match 用于与请求匹配的附加闭包。
参数
string $method

正在模拟的 HTTP 方法。

string $url

正在匹配的 URL。有关示例,请参见上文。

Cake\Http\Client\Response $response

与请求匹配的响应。

array<string, mixed> $options optional

见上文。

返回值
void

buildUrl() ¶ public

buildUrl(string $url, array|string $query = [], array<string, mixed> $options = []): string

根据范围化的客户端选项生成 URL。

参数
string $url

完整的 URL 或只是路径。

array|string $query optional

URL 的查询数据。

array<string, mixed> $options optional

与 Client::config() 存储在一起的配置选项

返回值
string

clearMockResponses() ¶ public static

clearMockResponses(): void

清除所有模拟响应

返回值
void

configShallow() ¶ public

configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this

将提供的配置与现有配置合并。与 `config()` 递归合并嵌套键不同,此方法进行简单合并。

设置特定值

$this->configShallow('key', $value);

设置嵌套值

$this->configShallow('some.nested.key', $value);

同时更新多个配置设置

$this->configShallow(['one' => 'value', 'another' => 'value']);
参数
array<string, mixed>|string $key

要设置的键或完整的配置数组。

mixed|null $value optional

要设置的值。

返回值
$this

cookies() ¶ public

cookies(): Cake\Http\Cookie\CookieCollection

获取 Client 中存储的 Cookie。

返回值
Cake\Http\Cookie\CookieCollection

createFromUrl() ¶ public static

createFromUrl(string $url): static

返回的 Client 实例的范围限定于从传入的 URL 字符串解析的域、端口和方案。传入的字符串必须包含方案和域。可选地,如果字符串中包含端口,则端口也会被范围限定。如果 URL 中包含路径,则 Client 实例将在其前面添加路径构建 URL。URL 字符串的其他部分将被忽略。

参数
string $url

字符串 URL 例如 https://example.com

返回值
static
抛出
InvalidArgumentException

delete() ¶ public

delete(string $url, mixed $data = [], array<string, mixed> $options = []): Cake\Http\Client\Response

执行 DELETE 请求。

参数
string $url

您要请求的 URL 或路径。

mixed $data optional

您要发送的请求数据。

array<string, mixed> $options optional

请求的附加选项。

返回值
Cake\Http\Client\Response

dispatchEvent() ¶ public

dispatchEvent(string $name, array $data = [], TSubject|null $subject = null): Cake\Event\EventInterface<TSubject>

创建和分发事件的包装器。

返回已分派的事件。

参数
string $name

事件的名称。

array $data optional

您希望随此事件一起传输的任何值,监听器都可以读取。

TSubject|null $subject optional

此事件适用的对象(默认情况下为 $this)。

返回值
Cake\Event\EventInterface<TSubject>

get() ¶ public

get(string $url, array|string $data = [], array<string, mixed> $options = []): Cake\Http\Client\Response

执行 GET 请求。

$data 参数支持特殊的 _content 键,用于在 GET 请求中提供请求主体。这通常不使用,但像 ElasticSearch 这样的服务使用此功能。

参数
string $url

您要请求的 URL 或路径。

array|string $data optional

您要发送的查询数据。

array<string, mixed> $options optional

请求的附加选项。

返回值
Cake\Http\Client\Response

getConfig() ¶ public

getConfig(string|null $key = null, mixed $default = null): mixed

返回配置。

用法

读取整个配置

$this->getConfig();

读取特定值

$this->getConfig('key');

读取嵌套值

$this->getConfig('some.nested.key');

读取默认值

$this->getConfig('some-key', 'default-value');
参数
string|null $key optional

要获取的键,或整个配置的 null。

mixed $default optional

键不存在时的返回值。

返回值
mixed

getConfigOrFail() ¶ public

getConfigOrFail(string $key): mixed

返回此特定键的配置。

此键的配置值必须存在,它永远不能为 null。

参数
string $key

要获取的键。

返回值
mixed
抛出
InvalidArgumentException

getEventManager() ¶ public

getEventManager(): Cake\Event\EventManagerInterface

返回此对象的 Cake\Event\EventManager 管理器实例。

您可以使用此实例将任何新的监听器或回调注册到对象事件,或者创建自己的事件并随意触发它们。

返回值
Cake\Event\EventManagerInterface

head() ¶ public

head(string $url, array $data = [], array<string, mixed> $options = []): Cake\Http\Client\Response

执行 HEAD 请求。

参数
string $url

您要请求的 URL 或路径。

array $data optional

您要发送的查询字符串数据。

array<string, mixed> $options optional

请求的附加选项。

返回值
Cake\Http\Client\Response

options() ¶ public

options(string $url, mixed $data = [], array<string, mixed> $options = []): Cake\Http\Client\Response

执行 OPTIONS 请求。

参数
string $url

您要请求的 URL 或路径。

mixed $data optional

您要发送的请求数据。

array<string, mixed> $options optional

请求的附加选项。

返回值
Cake\Http\Client\Response

patch() ¶ public

patch(string $url, mixed $data = [], array<string, mixed> $options = []): Cake\Http\Client\Response

执行 PATCH 请求。

参数
string $url

您要请求的 URL 或路径。

mixed $data optional

您要发送的请求数据。

array<string, mixed> $options optional

请求的附加选项。

返回值
Cake\Http\Client\Response

post() ¶ public

post(string $url, mixed $data = [], array<string, mixed> $options = []): Cake\Http\Client\Response

执行 POST 请求。

参数
string $url

您要请求的 URL 或路径。

mixed $data optional

您要发送的发布数据。

array<string, mixed> $options optional

请求的附加选项。

返回值
Cake\Http\Client\Response

put() ¶ public

put(string $url, mixed $data = [], array<string, mixed> $options = []): Cake\Http\Client\Response

执行 PUT 请求。

参数
string $url

您要请求的 URL 或路径。

mixed $data optional

您要发送的请求数据。

array<string, mixed> $options optional

请求的附加选项。

返回值
Cake\Http\Client\Response

send() ¶ public

send(Psr\Http\Message\RequestInterface $request, array<string, mixed> $options = []): Cake\Http\Client\Response

发送请求。

在其他方法中内部使用,但也可以用于发送手工制作的 Request 对象。

参数
Psr\Http\Message\RequestInterface $request

要发送的请求。

array<string, mixed> $options optional

要使用的其他选项。

返回值
Cake\Http\Client\Response

sendRequest() ¶ public

sendRequest(RequestInterface $request): Psr\Http\Message\ResponseInterface

发送 PSR-7 请求并返回 PSR-7 响应。

参数
RequestInterface $request

Request 实例。

返回值
Psr\Http\Message\ResponseInterface
抛出
Psr\Http\Client\ClientExceptionInterface
如果在处理请求时发生错误。

setConfig() ¶ public

setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this

设置配置。

用法

设置特定值

$this->setConfig('key', $value);

设置嵌套值

$this->setConfig('some.nested.key', $value);

同时更新多个配置设置

$this->setConfig(['one' => 'value', 'another' => 'value']);
参数
array<string, mixed>|string $key

要设置的键或完整的配置数组。

mixed|null $value optional

要设置的值。

bool $merge optional

是否递归合并或覆盖现有配置,默认为 true。

返回值
$this
抛出
Cake\Core\Exception\CakeException
当尝试设置无效的键时。

setEventManager() ¶ public

setEventManager(Cake\Event\EventManagerInterface $eventManager): $this

返回此对象的 Cake\Event\EventManagerInterface 实例。

您可以使用此实例将任何新的监听器或回调注册到对象事件,或者创建自己的事件并随意触发它们。

参数
Cake\Event\EventManagerInterface $eventManager

要设置的事件管理器

返回值
$this

trace() ¶ public

trace(string $url, mixed $data = [], array<string, mixed> $options = []): Cake\Http\Client\Response

执行 TRACE 请求。

参数
string $url

您要请求的 URL 或路径。

mixed $data optional

您要发送的请求数据。

array<string, mixed> $options optional

请求的附加选项。

返回值
Cake\Http\Client\Response

属性详情

$_adapter ¶ protected

用于发送请求的适配器。

类型
Cake\Http\Client\AdapterInterface

$_config ¶ protected

运行时配置

类型
array<string, mixed>

$_configInitialized ¶ protected

配置属性是否已使用默认值进行配置

类型
bool

$_cookies ¶ protected

使用此客户端执行的响应中的 Cookie 列表。

Cookie 按 Cookie 的域名或请求主机名索引。

类型
Cake\Http\Cookie\CookieCollection

$_defaultConfig ¶ protected

客户端的默认配置。

类型
array<string, mixed>

$_eventClass ¶ protected

新事件对象的默认类名。

类型
string

$_eventManager ¶ protected

此对象用来分发内部事件的 Cake\Event\EventManager 实例。

类型
Cake\Event\EventManagerInterface|null

$_mockAdapter ¶ protected static

用于在测试中存根请求的模拟适配器。

类型
Cake\Http\Client\Adapter\Mock|null
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 手册
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识 & 商标
  • 社区
  • 参与进来
  • 问题 (Github)
  • Bakery
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 时事通讯
  • 领英
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助 & 支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成