奇怪的现象,api访问超时

wangsky522

问题描述

浏览器能访问接口 但是apifox就超时了,对接了三方,三方的回调接口也是超时,应该不是代码层面的原因,但是不清楚怎么排查了。

程序代码或配置

#[ApiDoc\Title("通话回调")]
    #[ApiDoc\Method("GET")]
    #[ApiDoc\Url("api/callback/call-result")]
    public function callResult(Request $request): Response
    {
        try {
            error_log("=== 通话回调开始 ===");
            error_log("请求方法: " . $request->method());
            error_log("请求URL: " . $request->url());
            error_log("请求头: " . json_encode($request->header()));

            $data = $request->all();
            error_log("接收到的数据: " . json_encode($data, JSON_UNESCAPED_UNICODE));

            // 验证核心必填字段(放宽验证)
            $coreFields = ['jobId', 'customerPhone', 'startedAt', 'duration'];
            foreach ($coreFields as $field) {
                if (!isset($data[$field])) {
                    error_log("缺少核心字段: {$field}");
                    // 不抛出异常,记录日志即可
                }
            }

            // 查找对应的客户 - 尝试多种匹配方式
            $customer = null;
            $searchPhones = [
                $data['customerPhone'] ?? null,
                $data['phone'] ?? null,
            ];

            error_log("尝试匹配的电话号码: " . json_encode($searchPhones));

            foreach ($searchPhones as $phone) {
                if ($phone) {
                    $customer = Customer::where('phone', $phone)->first();
                    if ($customer) {
                        error_log("通过电话 {$phone} 找到客户: ID={$customer->id}");
                        break;
                    }
                }
            }

            if (!$customer) {
                error_log("客户不存在,尝试的电话: " . json_encode($searchPhones));
                error_log("数据库中所有客户电话: " . json_encode(Customer::pluck('phone')->toArray()));

                // 即使找不到客户,也返回成功,避免回调失败
                error_log("=== 通话回调处理完成(客户未找到) ===");
                return json(['code' => 0, 'msg' => 'ok', 'data' => ['customer_not_found' => true]]);
            }

            error_log("找到客户: ID={$customer->id}, 电话={$customer->phone}");

            // 检查是否已存在相同的通话记录
            $jobId = $data['jobId'] ?? null;
            if ($jobId) {
                $existingLog = CallLog::where('job_id', $jobId)->first();
                if ($existingLog) {
                    error_log("更新现有通话记录: job_id={$jobId}");
                    // 更新现有记录
                    $this->updateCallLog($existingLog, $data);
                } else {
                    error_log("创建新通话记录: job_id={$jobId}");
                    // 创建新的通话记录
                    $callLog = $this->createCallLog($data, $customer);
                    error_log("通话记录创建成功: ID={$callLog->id}");
                }
            }

            // 更新客户状态
            $vadStatus = $data['vadStatus'] ?? 0;
            error_log("更新客户状态: vadStatus={$vadStatus}");
            $this->updateCustomerStatus($customer, $data);

            error_log("=== 通话回调处理完成 ===");
            return json(['code' => 0, 'msg' => 'ok', 'data' => []]);

        } catch (\Exception $e) {
            // 记录错误日志
            error_log("=== 通话回调错误 ===");
            error_log("错误信息: " . $e->getMessage());
            error_log("错误文件: " . $e->getFile());
            error_log("错误行号: " . $e->getLine());
            error_log("错误堆栈: " . $e->getTraceAsString());
            error_log("=== 通话回调错误结束 ===");
            return json(['code' => 1, 'msg' => 'error', 'data' => []]);
        }
    }

重现问题的步骤

截图
截图

操作系统环境及workerman/webman等具体版本

webman

308 3 0
3个回答

Tinywan

一个是http,一个是https

  • wangsky522 9天前

    都用http也是一样的,更奇怪的是用ip可以访问接口

  • Tinywan 9天前

    那就是你配置问题

  • nitron 9天前

    看下是不是对request header做了限制,比如user-agent之类的

morris

都复制成curl 请求。 对比一下实际数据 哪里不一样 。

  • 暂无评论
ichynul

可能域名解析问题,遇到过泛解析到ip,没加www的解析到ip。
然后浏览器可以用www访问,微信支付回调不行,ping也不行,后来单独加上www解析就正常。

  • 暂无评论
🔝