«

PHP cURL请求超时,超时时间设置

时间:2026-10-9 07:30     作者:emer     分类: 无


前言

PHP用cURL请求接口,结果一直卡住或者超时?cURL默认没有超时时间,对方服务器慢的话能卡你几分钟。必须设超时,不然整个脚本都堵死了。本文讲怎么设。

一、为什么要设超时

不设超时的后果:

  1. 对方服务器挂了,你的脚本一直等着
  2. 对方服务器慢,你的脚本卡很久
  3. 高并发下,进程都堵在cURL上,服务器卡死

一定要设!

二、两个超时参数

1. CURLOPT_CONNECTTIMEOUT

连接超时,单位秒。

就是TCP连接花多久。

推荐: 5-10秒

2. CURLOPT_TIMEOUT

总超时,单位秒。

整个请求(连接+发送+接收)花多久。

推荐: 30-60秒

区别

参数 含义 推荐值
CURLOPT_CONNECTTIMEOUT 连接超时 5秒
CURLOPT_TIMEOUT 总超时 30秒

三、完整示例

<?php
$url = 'https://api.example.com/data';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 连接超时(秒)
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

// 总超时(秒)
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

// 毫秒级超时(可选)
// curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 5000);
// curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'cURL错误: ' . curl_error($ch);
}

curl_close($ch);

echo $result;

四、超时错误码

常见错误码

错误码 说明
28 超时(CURLE_OPERATION_TIMEDOUT)
7 连接失败(CURLE_COULDNT_CONNECT)
6 域名解析失败(CURLE_COULDNT_RESOLVE_HOST)

检查错误码

if (curl_errno($ch) == 28) {
    echo "请求超时了";
}

五、常见坑

坑1:timeout设成了毫秒

CURLOPT_TIMEOUT的单位是秒,不是毫秒!

错误:

curl_setopt($ch, CURLOPT_TIMEOUT, 5000); // 5000秒,不是5秒!

正确:

curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 5秒

或者用毫秒版:

curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000); // 5000毫秒 = 5秒

坑2:set_time_limit不够

PHP脚本本身有最大执行时间。

如果cURL超时30秒,但PHP脚本只允许10秒,PHP先超时了。

解决:

set_time_limit(0); // 不限制
// 或者
set_time_limit(60); // 60秒

坑3:DNS解析慢

域名解析也要时间,算在连接超时里。

解决: 连接超时设长一点,比如10秒。

坑4:https证书问题

https请求如果证书有问题,也会卡住或报错。

临时关闭验证:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

注意: 生产环境不建议关,最好把证书配好。

坑5:重定向太多

对方返回301/302重定向,cURL默认不自动跳。

开启自动跳转:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3); // 最多跳3次

六、POST请求示例

<?php
$url = 'https://api.example.com/submit';
$data = [
    'name' => '张三',
    'age' => 20
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo '错误: ' . curl_error($ch);
}

curl_close($ch);

echo $result;

七、封装成函数

function http_curl($url, $post = false, $data = [], $timeout = 30)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    if ($post) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
    }

    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        $error = curl_error($ch);
        curl_close($ch);
        return ['code' => -1, 'msg' => $error];
    }

    curl_close($ch);
    return ['code' => 0, 'data' => $result];
}

// 使用
$res = http_curl('https://api.example.com/data');
if ($res['code'] == 0) {
    echo $res['data'];
} else {
    echo '请求失败: ' . $res['msg'];
}

八、推荐配置

普通接口请求

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);  // 5秒连接
curl_setopt($ch, CURLOPT_TIMEOUT, 30);        // 30秒总超时

快速接口(查个数据)

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);  // 3秒连接
curl_setopt($ch, CURLOPT_TIMEOUT, 10);        // 10秒总超时

慢接口(上传大文件)

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 10秒连接
curl_setopt($ch, CURLOPT_TIMEOUT, 120);       // 2分钟总超时

总结

记住:

  1. cURL一定要设超时,不然卡死
  2. CURLOPT_CONNECTTIMEOUT 连接超时(秒)
  3. CURLOPT_TIMEOUT 总超时(秒)
  4. 单位是秒,不是毫秒
  5. 超时了用 curl_errno 判断是不是28

最常用:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

遇到问题加QQ23979811 协助处理

标签: PHP cURL 超时设置 CURLOPT_TIMEOUT 接口请求