sopheos / pebble_http

请求/响应库

0.1.0 2024-08-20 06:10 UTC

This package is auto-updated.

Last update: 2024-09-20 06:19:36 UTC


README

PHP的抽象层,用于分析HTTP请求并创建HTTP响应。

请求

Pebble\Http\Request 获取HTTP请求的数据。

服务器数据

  • servers(): array 返回服务器参数数组($_SERVER)。
  • server(string $name) 返回服务器参数。
  • time(): int 返回请求的时标(以秒为单位)。
  • mtime(): float 返回请求的时标(以秒为单位),具有微秒级精度。

HTTP方法

method(): string 返回请求的HTTP方法(CLI、GET、POST、DELETE等)。isGet(): bool 对于HTTP GET请求返回true。isPost(): bool 对于HTTP POST请求返回true。isPut(): bool 对于HTTP PUT请求返回true。isPatch(): bool 对于HTTP PATCH请求返回true。isDelete(): bool 对于HTTP DELETE请求返回true。isClient(): bool 对于来自命令行的请求返回true。isAjax(): bool 对于AJAX请求返回true。isSecure(): bool 对于HTTPS请求返回true。uri(): string 返回请求的URI。

Cookie

  • cookies(): array 返回Cookie数组。
  • cookie(string $name) 返回Cookie的值。

请求数据

  • queryParams(): array 返回请求参数数组。
  • queryParam(string $name) 返回数据参数的值。
  • bodyParams(): array 返回表单或JSON请求体的数据数组。
  • bodyParam(string $name) 返回表单或JSON请求体的数据值。
  • attachments(): array 返回上传文件的数据数组。按字段名索引。
  • attachment(string $name): array 返回上传文件的数据。

客户端

  • userAgent(): string 返回用户代理。
  • ip(): string 返回IP地址。

响应

Pebble\Http\Response 创建HTTP响应。

  • reset(): mixed 重置响应。

HTTP状态码

  • setProtocolVersion(): string 设置HTTP协议版本。默认使用请求的版本。
  • setStatusCode(int $code, ?string $reason = null): static 设置响应的HTTP状态码。如果理由为null,将使用与HTTP状态码对应的默认值。

HTTP头

  • addHeader(string $name, string $value): static 添加HTTP头。
  • removeHeader(string $name): static 删除HTTP头。
  • setContentType(string $mime, string $charset = "UTF-8"): static 设置内容类型。
    $res->setContentType('jpg'); // shortcurt
    $res->setContentType('image/jpeg'); // verbose
  • addCookie(string $name, $value, int $expire = 0, $settings = []): static 添加Cookie。
  • removeCookie(string $name): static 删除Cookie。
  • redirect(string $url = "/", bool $temporary = true): static HTTP重定向。
  • cache(int $age = 86400): static 用于配置HTTP缓存的快捷方式。
  • noCache(): static 禁用HTTP缓存。
  • cors(?string $origin = null, ?string $method = null): static 启用CORS。

HTTP体

  • setBody(Pebble\Http\Stream|string $body = ""): static 向响应添加内容。
  • setText(string $data = ''): static 将字符串转换为text/plain。
  • setJson(mixed $data = null): static 将数据转换为application/json。
  • setJsonException(ResponseException $ex): static 将 ResponseException 转换为 application/json 格式。

渲染

emitHeaders() 向浏览器发送头信息。emitBody(int $bufferLength = 0) 向浏览器发送内容。emit(int $bufferLength = 0) 向浏览器发送头信息和内容。

会话

Pebble\Http\Session 用于操作会话的抽象层。

  • __construct(\SessionHandlerInterface $handler = NULL) 构造函数接受可选的会话处理器(memcache、db 等)。
  • start(): static 开始会话。
  • close(): static 写入并关闭会话。
  • destroy(): static 销毁会话。
  • reset(): static 删除所有会话变量。
  • all() 返回所有数据。
  • has(string $name): bool 返回会话中是否存在数据。
  • get(string $name, $default = null): mixed 获取会话中的值。
  • set(string $name, mixed $value): static 向会话添加数据。

闪存数据

闪存数据存在于下一个请求之前(除非重新标记为闪存)。

  • setFlash(string $name, $value): static
  • markFlash(string $name): static
  • unmarkFlash(string $name): static

临时数据

临时数据存在一定的时间。

  • setTemp($name, $value, $time = 300)
  • markTemp(string $name, int $time = 300)
  • unmarkTemp(string $name)

异常

用于全局处理 HTTP 响应的错误。

ResponseException

继承自 Exception 并实现 JsonSerializable

setErrors(array $errors): static : 配置错误 addError(string $key, mixed $value): static : 添加错误。 setExtra(array $extra): static : 配置额外数据。 addExtra(string $key, mixed $value): static : 添加额外数据。

异常列表

继承自 ResponseException

  • AccessException : 认证信息无效(状态码 401)。
  • ForbiddenException : 禁止访问(状态码 403)。
  • EmptyException : 资源未找到(状态码 404)。
  • LockException : 资源被锁定。(状态码 423)。
  • UserException : 来自用户的错误。例如验证错误(状态码 400)。
  • SystemException : 执行错误。例如崩溃。 (状态码 500)。