最简POST请求需设CURLOPT_POST为true、CURLOPT_POSTFIELDS传数组或字符串,并务必设CURLOPT_RETURNTRANSFER为true;发JSON需json_encode()数据+手动设Content-Type头;生产环境须配置超时、重定向和SSL验证。

用 curl_init() 发起最简 POST 请求
PHP 模拟 POST 请求,curl 是最常用也最可控的方式。不依赖外部扩展(只要 cURL 模块启用),适合绝大多数场景。
关键点在于设置 CURLOPT_POST 为 true,并用 CURLOPT_POSTFIELDS 传数据——注意:传数组会自动编码为 application/x-www-form-urlencoded;传字符串则需手动设 Content-Type 头。
常见错误:漏掉 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true),导致响应直接输出而非返回字符串。
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://httpbin.org/post'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, ['name' => 'Alice', 'age' => 25]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch);
发送 JSON 格式的 POST 请求
调用 API 时经常要发 application/json,这时候不能直接把数组塞给 CURLOPT_POSTFIELDS,否则服务端收不到标准 JSON。
立即学习“PHP免费学习笔记(深入)”;
必须:json_encode() 数据 + 手动设置 Content-Type 头 + 确保 CURLOPT_POSTFIELDS 接收的是字符串。
- 忘记设
Content-Type:服务端可能按表单解析,字段全为空 - 用
http_build_query()替代json_encode():结果是 URL 编码字符串,不是 JSON - 没检查
curl_exec()返回值是否为false:网络失败时会静默返回空,难排查
$data = ['title' => 'Hello', 'content' => 'World'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://httpbin.org/post');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
echo 'cURL error: ' . curl_error($ch);
}
curl_close($ch);
处理重定向、超时和 SSL 验证
生产环境发请求,不加基础防护容易卡死或报错。默认 cURL 不跟随重定向、SSL 验证严格,本地调试常被忽略,上线后突然失败。
三个最该加的配置:
-
CURLOPT_FOLLOWLOCATION设为true:避免 301/302 后返回空响应 -
CURLOPT_TIMEOUT显式设超时(如10秒):防止对方接口挂了拖垮你的脚本 -
CURLOPT_SSL_VERIFYPEER和CURLOPT_SSL_VERIFYHOST:线上必须为true;开发环境若用自签名证书,可临时设false,但绝不提交到代码库
不用 cURL 的替代方案:file_get_contents() + stream_context_create()
如果服务器禁用了 cURL(少见但存在),可用原生流函数。写法稍冗长,但无需额外扩展。
核心是构造 stream_context,把 method 设为 POST,content 填入请求体,header 手动拼好。
易错点:
-
content必须是字符串,JSON 要先json_encode() -
header字符串里换行必须用\n,不能用\r\n或空行 - 没有内置重试或连接池,异常处理全靠
try/catch包裹file_get_contents()
$data = json_encode(['id' => 123]);
$opts = [
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/json\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
]
];
$context = stream_context_create($opts);
$result = file_get_contents('https://httpbin.org/post', false, $context);
实际用的时候,CURLOPT_POSTFIELDS 的类型判断、Content-Type 与数据格式的匹配、以及 SSL 验证开关的条件控制,才是最容易出问题的三处。别图省事硬编码 false,也别在没测清服务端要求前瞎猜 header。











