HTTP 方法与用途速览
- GET:读取资源的表示(查询、获取详情、下载)
- HEAD:获取资源的元信息(只要响应头,不要响应体)
- POST:提交数据进行处理或在集合下创建子资源
- PUT:按指定 URI 全量创建或替换资源
- PATCH:对资源进行部分更新
- DELETE:删除指定资源
- OPTIONS:查询目标资源支持的通信选项(常见于 CORS 预检)
- TRACE:回显服务器收到的请求(诊断用,生产常禁用)
- CONNECT:通过代理建立到目标的隧道(HTTPS 走代理场景,应用层很少直接使用)
GET(读取资源)
<?php
$ch = curl_init('https://api.example.com/items/123');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP $code\n";
echo $response;
HEAD(仅获取响应头)
<?php
$ch = curl_init('https://api.example.com/items/123');
curl_setopt($ch, CURLOPT_NOBODY, true); // 不要响应体
curl_setopt($ch, CURLOPT_HEADER, true); // 返回响应头
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP $code\n";
echo $headers;
POST(提交数据/创建子资源)
<?php
$payload = json_encode(['name' => 'foo', 'price' => 12.5]);
$ch = curl_init('https://api.example.com/items');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json',
]);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$location = curl_getinfo($ch, CURLINFO_REDIRECT_URL); // 若服务器返回 Location,可通过响应头读取
curl_close($ch);
echo "HTTP $code\n";
echo $response;
PUT(按 URI 全量替换/创建资源)
<?php
$payload = json_encode(['id' => 123, 'name' => 'bar', 'price' => 10.0]);
$ch = curl_init('https://api.example.com/items/123');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
// 如需并发控制,可带条件头避免覆盖:
// 'If-Match: "abc123etag"',
]);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP $code\n";
echo $response;
PATCH(部分更新)
<?php
// 以 JSON Merge Patch 为例
$payload = json_encode(['price' => 9.99]);
$ch = curl_init('https://api.example.com/items/123');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/merge-patch+json',
'Accept: application/json',
]);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP $code\n";
echo $response;
DELETE(删除资源)
<?php
$ch = curl_init('https://api.example.com/items/123');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP $code\n";
echo $response;
OPTIONS(查询支持的方法/CORS 预检)
<?php
$ch = curl_init('https://api.example.com/items');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'OPTIONS');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 也可添加 CORS 预检相关请求头以查看服务端返回的允许策略
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Origin: https://app.example.com',
'Access-Control-Request-Method: POST',
'Access-Control-Request-Headers: Content-Type',
]);
$headers = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP $code\n";
echo $headers; // 留意 Allow、Access-Control-Allow-* 等响应头
TRACE(诊断回显,常被禁用)
<?php
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'TRACE');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP $code\n";
echo $response; // 若服务器禁用 TRACE,可能返回 405/501 等
CONNECT(通过代理建立隧道,常用于 HTTPS 代理)
说明:CONNECT 主要由客户端与代理交互以建立到目标服务器的隧道,应用层通常不直接“向业务服务”发送 CONNECT。PHP cURL 在走代理访问 HTTPS 时会在底层使用 CONNECT 建隧道。
<?php
// 通过 HTTP 代理访问 HTTPS 目标,cURL 底层会使用 CONNECT 建立隧道
$ch = curl_init('https://www.example.com/');
curl_setopt($ch, CURLOPT_PROXY, 'http://proxy.example.com:3128');
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true); // 使用隧道(CONNECT)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP $code\n";
echo $response;
如果你有具体接口或返回格式,我可以按你的字段与约定,进一步完善每个示例的请求头、错误处理与响应解析。
HTTP 状态码基本概念
- HTTP 响应包含状态行(如 HTTP/1.1 200 OK),其中的三位数字即“状态码”,表示服务器对请求的处理结果。
- 代码分为五大类:1xx 信息、2xx 成功、3xx 重定向、4xx 客户端错误、5xx 服务器错误。
- 语义参考标准:RFC 9110(更新并取代早期 RFC 7231)。
类别概览
- 1xx 信息:请求已接收,继续处理(较少直接使用)
- 2xx 成功:请求成功并返回结果或执行完成
- 3xx 重定向:客户端需采取额外动作(通常跟随新 URI)
- 4xx 客户端错误:请求有问题(参数、认证、方法等)
- 5xx 服务器错误:服务器处理失败或上游故障
常见状态码
1xx 信息
- 100 Continue:客户端可继续发送请求体(分块上传前的确认)
- 101 Switching Protocols:协议切换(如升级到 WebSocket)
- 103 Early Hints:提前发送链接提示,优化页面加载
2xx 成功
- 200 OK:常规成功,通常带响应体
- 201 Created:资源已创建;应在响应头
Location 给出新资源 URI
- 202 Accepted:请求已接受但未完成(异步处理)
- 204 No Content:成功但无响应体(常用于 PUT/PATCH/DELETE)
- 206 Partial Content:部分内容(断点续传/范围请求)
3xx 重定向
- 301 Moved Permanently:永久重定向(SEO 友好;改用新 URI)
- 302 Found:临时重定向(历史用法混乱,现代常用 303/307)
- 303 See Other:重定向到 GET(表单提交后跳详情页)
- 304 Not Modified:资源未变更(配合 ETag/Last-Modified 的条件 GET)
- 307 Temporary Redirect:临时重定向,保持方法不变
- 308 Permanent Redirect:永久重定向,保持方法不变
4xx 客户端错误
- 400 Bad Request:请求格式/参数错误
- 401 Unauthorized:未认证或认证失败;应带
WWW-Authenticate 提示
- 403 Forbidden:已认证但无权限
- 404 Not Found:资源不存在或不暴露
- 405 Method Not Allowed:方法不被目标资源支持;应带
Allow 列表
- 409 Conflict:状态冲突(版本、业务约束),常用于并发写冲突
- 410 Gone:资源已永久移除(区别于 404 的明确语义)
- 412 Precondition Failed:条件请求失败(如
If-Match 不匹配)
- 415 Unsupported Media Type:请求体类型不被接受(如 Content-Type 不支持)
- 422 Unprocessable Content(或 Unprocessable Entity):语义校验失败(字段合法但不满足规则)
- 429 Too Many Requests:限流命中;可配
Retry-After 提示重试时间
5xx 服务器错误
- 500 Internal Server Error:通用服务器错误(未分类异常)
- 501 Not Implemented:服务器不支持该方法或功能
- 502 Bad Gateway:网关/代理从上游收到无效响应
- 503 Service Unavailable:服务不可用(维护/过载);可配
Retry-After
- 504 Gateway Timeout:网关/代理等待上游超时
设计与使用要点
- 创建用 201 +
Location;更新或删除成功可用 200/204。
- 条件读写用 304/412 搭配 ETag,提升缓存与并发安全。
- 区分 401 与 403:前者是认证问题,后者是授权问题。
- 重定向选择:永久 301/308,临时 302/307;提交后跳转用 303。
- 限流和维护:429/503 搭配
Retry-After,便于客户端退避重试。
- 方法不支持返回 405,并在
Allow 列出支持的方法,提升可发现性。