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

如何在CodeIgniter中使用指向localhost的Trello Webhook回调URL?

你的方向完全正确,不过localhost回调需要调整!

嘿,首先得说:用Trello Webhook来监听特定看板的新卡片创建事件,绝对是实现你需求的正确方向——这是Trello官方推荐的实时事件通知方案,比定时轮询看板高效太多了。

关于localhost回调的问题

直接把回调URL设为localhost/main_controller/trelloCallback是无效的,原因很简单:Trello的服务器部署在公网上,它无法访问你本地电脑上的localhost服务(本地服务只在你的内网环境里可访问)。

解决方法:用内网穿透工具暴露本地服务

你需要一个工具把本地的CodeIgniter服务映射成公网可访问的地址,最常用的就是ngrok:

  • 下载并安装ngrok后,在终端运行命令:ngrok http 8080(这里的8080换成你本地服务的端口,比如XAMPP默认是80)
  • 运行后会得到一个类似https://xxxx-xx-xx-xx-xx.ngrok.io的公网URL
  • 把这个URL加上你的回调路径,比如https://xxxx-xx-xx-xx-xx.ngrok.io/main_controller/trelloCallback,作为Trello Webhook的回调地址即可

实现Webhook的关键要点

  1. Webhook验证
    Trello在创建Webhook时会先发送一个HEAD请求到你的回调URL,你需要返回200 OK状态码才能通过验证。如果没处理这个HEAD请求,Trello会认为你的回调地址不可用,创建Webhook会失败。

  2. 精准监听事件
    创建Webhook时,指定你要监听的看板ID,并且设置只监听createCard事件(在Webhook的callbackURL参数之外,还要指定idModel为看板ID,以及filter为createCard),这样Trello只会在该看板创建新卡片时推送通知,避免无关请求。

  3. 验证请求合法性
    为了确保推送请求确实来自Trello,你可以用Trello提供的签名验证机制:对比请求头X-Trello-Webhook的签名和你用自己的Trello密钥计算出的签名,防止伪造请求。

简单的CodeIgniter控制器示例

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main_controller extends CI_Controller {

    public function trelloCallback() {
        // 处理Trello的HEAD验证请求
        if ($this->input->method() === 'head') {
            $this->output->set_status_header(200);
            return;
        }

        // 处理POST事件推送
        $eventRaw = $this->input->raw_input_stream;
        $eventData = json_decode($eventRaw, true);

        // 只处理新卡片创建事件
        if ($eventData['action']['type'] === 'createCard') {
            // 提取卡片信息
            $cardInfo = $eventData['action']['data']['card'];
            $cardId = $cardInfo['id'];
            $cardName = $cardInfo['name'];
            $boardId = $eventData['action']['data']['board']['id'];

            // 这里写你的业务逻辑:比如记录到数据库、触发系统内部通知等
            // $this->db->insert('trello_cards', ['card_id' => $cardId, 'name' => $cardName]);
        }

        // 必须返回200,否则Trello会重复推送
        $this->output->set_status_header(200);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:12:51