You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel控制器发送SMS失败:Undefined variable $ch错误求助

Laravel控制器调用短信API报错及修复方案

一、直接修复Undefined variable $ch错误

代码中定义的cURL句柄是$curl,但后续设置POST字段时错误使用了未定义的$ch变量,只需将curl_setopt($ch, CURLOPT_POSTFIELDS, ...)中的$ch替换为$curl即可。

二、梳理短信API调用逻辑问题

你的代码同时混用了原生cURL和Laravel的Http门面,且API调用逻辑混乱(GET参数拼接与POST请求冲突),以下提供两种规范的修复方案:

方案1:统一使用原生cURL

if ($isInsertSuccess) {
    $access_token = 'eyJhbGciOiJIUfgsFSdghjkhJHjhKJHjhzUxMiJ9.eyJzdWIiOiJkbXJjYWhhamphZEBnbWFpbC5jb20iLCJ0eXBlIjoiYWNjZXNzVG9rZW4iLCJleHAiOjE3MDE0Mjc3MDMsImlhdCI6MTcwMTM0MTMwM30.b8Mm8oMDLl-_dQB9dgfpbVIgdeJ8E6y4CG7N_aOB_Z3A5jTGSBjY7Y5d5vcnzyhoxfwWQxridlC74aqGHm2nIQ';

    $post_data = [
        "numbers" => $mob1,
        "content" => "2024 Reference XXX Application. Reg. No - $registration_id Your application is being processed & it is pending. Once the process is completed, we will confirm you.",
    ];

    // 初始化cURL,直接调用API的POST端点
    $curl = curl_init('https://bsms.hutch.lk/api/sendsms');
    
    // 设置cURL选项
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Accept: */*',
        'X-API-VERSION: v1',
        'Authorization: Bearer ' . $access_token
    ]);
    // 适配JSON格式的请求体
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));

    $response = curl_exec($curl);

    if ($response) {
        $notification = [
            'message' => 'Application Registered Successfully',
            'alert-type' => 'success',
        ];
        return redirect()->back()->with($notification);
    } else {
        // 错误信息写入日志,避免直接输出破坏响应
        \Log::error('SMS发送失败: ' . curl_error($curl));
        $notification = [
            'message' => 'Application Registered But SMS Failed',
            'alert-type' => 'error',
        ];
        return redirect()->back()->with($notification);
    }

    curl_close($curl);
}

方案2:使用Laravel Http门面(更符合框架规范)

Laravel的Http门面已封装cURL,代码更简洁易维护:

if ($isInsertSuccess) {
    $access_token = 'eyJhbGciOiJIUfgsFSdghjkhJHjhKJHjhzUxMiJ9.eyJzdWIiOiJkbXJjYWhhamphZEBnbWFpbC5jb20iLCJ0eXBlIjoiYWNjZXNzVG9rZW4iLCJleHAiOjE3MDE0Mjc3MDMsImlhdCI6MTcwMTM0MTMwM30.b8Mm8oMDLl-_dQB9dgfpbVIgdeJ8E6y4CG7N_aOB_Z3A5jTGSBjY7Y5d5vcnzyhoxfwWQxridlC74aqGHm2nIQ';

    $response = Http::withHeaders([
        'Content-Type' => 'application/json',
        'Accept' => '*/*',
        'X-API-VERSION' => 'v1',
        'Authorization' => 'Bearer ' . $access_token
    ])->post('https://bsms.hutch.lk/api/sendsms', [
        "numbers" => $mob1,
        "content" => "2024 Reference XXX Application. Reg. No - $registration_id Your application is being processed & it is pending. Once the process is completed, we will confirm you.",
    ]);

    if ($response->successful()) {
        $notification = [
            'message' => 'Application Registered Successfully',
            'alert-type' => 'success',
        ];
        return redirect()->back()->with($notification);
    } else {
        \Log::error('SMS发送失败: ' . $response->body());
        $notification = [
            'message' => 'Application Registered But SMS Failed',
            'alert-type' => 'error',
        ];
        return redirect()->back()->with($notification);
    }
}

三、额外检查点

  • 确认服务器已启用cURL扩展:可通过phpinfo()查看状态
  • 验证$access_token是否有效、未过期
  • 核对短信API的参数要求:比如numbers是否需要加国家码、content是否有长度限制
  • 避免在控制器中使用echo,应通过Laravel响应机制返回结果
  • 建议将API配置(令牌、端点)存入.env文件,便于管理:
    SMS_API_TOKEN=your_token_here
    SMS_API_ENDPOINT=https://bsms.hutch.lk/api/sendsms
    
    代码中通过env('SMS_API_TOKEN')调用配置值

内容的提问来源于stack exchange,提问作者Mohamed rifky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 06:34:52