禅道 Rest API 开发

本文介绍如何在未使用PHP5.3及以上版本特性的旧PHP系统中,通过添加命名空间等新特性来实现与现代微服务框架(如Slim)的兼容。文章提供了具体的代码实例,展示如何在不破坏现有代码的基础上整合新功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在老的 PHP 系统中使用 PHP 5.3以后的库

所谓老的系统,是指没有使用PHP 5.3以上命名空间(namespace)特性编码的系统。

但是,只要你的系统运行在 PHP 5.3及以上的环境,在你的老系统中,是可以使用这些基于新特性如命名空间编码的库或代码的。

以前只是有洁癖不用而已。

比如,我是个工具控,想让所用的禅道系统也像那些国际化开源 Issue 项目一样有一套标准开放的 API - 禅道本身是有套 html、json 自适配模式可以当接口用的,可以用于其他客户端或系统集成。这几天在尝试编写的用于兼容 Redmine REST 风格接口的禅道 PMS API,就有意识的用了这种混合的写法。

由于要兼容 Redmine 的 REST 风格,首先选用了 Slim 这个微服务框架,毫无疑问,它是要求运行环境>5.3的,但我总得复用禅道已有的代码,这样效率才高。

原理很简单,就是一根反斜杠,或者两根。

先用 composer 初始化了 slim 库。

重点部位的代码:

入口文件 index.php

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/inc/zentao/nb/Autoloader.php';
\zentao\nb\Autoloader::register();

$app = \zentao\core\Application::app(dirname(ZTNB_ROOT)); //禅道的router

$slim = new \Slim\Slim();

$routes = require __DIR__ . '/data/config/routes.php';

foreach ($routes as $method => $_routes) {
    if ($_routes) {
        foreach ($_routes as $rule => $map) {
            $slim->$method($rule, '\\zentao\\nb\\resource\\' . $map);
        }
    }
}

$slim->run();

\zentao\core\Application 是独立封装的兼容禅道原来运行环境的类,由禅道中的 framework/router.class.php 改造而来,主要用于加载禅道中的相关资源如配置文件、模型等。精华应该在这里面,主要是加了一些“\”来让微服务中能跑起来禅道运来的运行环境,并作为一个命名空间的桥梁可以在新的代码中调用。

再看看资源类的父类 \zentao\nb\Resource,片段

<?php

namespace zentao\nb;

/**
 * 资源类 父类
 */
class Resource {

    public function __construct() {
        
    }

    /**
     * Load the model file of one module.
     * 
     * @param   string      $methodName    The method name, if empty, use current module's name.
     * @access  public
     * @return  object|bool If no model file, return false. Else return the model object.
     */
    protected function loadModel($moduleName) {
        $modelFile = \helper::setModelFile($moduleName);

        /* If no model file, try load config. */
        if (!\helper::import($modelFile)) {
            $this->app->loadConfig($moduleName, false);
            $this->app->loadLang($moduleName);
            $this->dao = new dao();
            return false;
        }

        $modelClass = class_exists('ext' . $moduleName . 'model') ? 'ext' . $moduleName . 'model' : $moduleName . 'model';
        $modelClass = '\\' . $modelClass;
        if (!class_exists($modelClass))
            $this->app->triggerError(" The model $modelClass not found", __FILE__, __LINE__, $exit = true);

        $this->$moduleName = new $modelClass();
        $this->dao = $this->$moduleName->dao;
        return $this->$moduleName;
    }
    

这样可以在资源类中调用禅道的 model 类。

还有另外一种用法,加载语言包:

<?php

namespace zentao\nb\resource;

use zentao\nb\enum\BugType;

/**
 * 项目自行定义的问题分类
 */
class IssueCategory extends \zentao\nb\resource {
    
    public function fetchAll($format = 'json') {
        global $app;

        $types = $app->loadLang('bug')->bug->typeList;
        $issue_categories = array();
        foreach ($types as $key => $name) {
            
            $issue_categories[] = array('id' => BugType::getIdByInterId($key), 'name' => $name);
        }
        
        echo json_encode(array('issue_categories' => $issue_categories));
    }
    
    /**
     *   根据项目来取其中定义的分类
     * @param int $projectId
     * @param string $format
     */
    public function fetchAllByProjectId($projectId, $format = 'json') {
        $model = $this->loadModel('project');
        $project = $model->getById($projectId);//TODO 支持按项目代号查找
        if (!$project) {
            $this->responseNotExixted();
        }
        
        global $app;
        $types = $app->loadLang('bug')->bug->typeList;
        $issue_categories = array();
        foreach ($types as $key => $name) {
            
            $issue_categories[] = array('id' => BugType::getIdByInterId($key), 'project' => array('id' => $projectId, 'name' => $project->name), 'name' => $name);
        }
        
        echo json_encode(array(
            'issue_categories' => $issue_categories,
            'total_count' => 2
        ));
    }
}

基本项目结构如下:

项目结构

代码:https://github.com/web3d/zentao-rest-api

项目只是初步成型,尚未完成。

5409-20151202114537343-1356995417.png

这是在 NB 中的任务列表。

5409-20151202114545546-1877111206.png

这是在 NB 中的任务详情。

<think>项目管理软件是一款国产的开源项目管理软件,集产品管理、项目管理、质量管理、文档管理、组织管理和事务管理于一体,是一款功能完备的项目管理软件,覆盖了项目管理的核心流程。的主要功能包括:产品管理、项目管理、质量管理(包括测试用例管理、Bug管理等)、文档管理、组织管理(包括团队管理和权限管理)以及事务管理(包括待办和日志等)。在C#领域,我们可能会涉及到与系统的集成,例如通过API进行数据的读取或写入。提供了丰富的API接口,允许开发者通过HTTP请求进行交互。常见的集成场景包括:1.同步中的项目、任务、Bug等数据到其他系统。2.在外部系统中创建任务或Bug。3.获取中的用户、产品、项目等信息。下面以C#代码示例,展示如何通过API进行登录并获取会话,然后获取产品列表。API通常需要先登录获取Session(或Token),然后使用该Session进行后续操作。###示例:使用C#调用API获取产品列表假设安装在本地,地址为:http://localhost:80,账号为:admin,密码为:123456。API调用通常有两种方式:一种是直接通过HTTPGET/POST请求,另一种是通过提供的SDK(如果有)。这里我们使用HTTP请求的方式。步骤:1.发送登录请求获取Session。2.使用获取到的Session调用获取产品列表的API。注意:API有两种风格:一种是传统的GET方式,参数在URL中;另一种是RESTful风格(需要专业版或以上版本)。这里以传统的GET方式为例。API的URL格式通常为:`http://{地址}/zentao/{module}-{method}-{params}.json`,其中:-module:模块名,如user,product,task等。-method:方法名,如login,getList等。-params:参数,如id=1,也可以没有参数。登录API:`http://localhost:80/zentao/user-login.json`登录需要POST账号密码。获取产品列表API:`http://localhost:80/zentao/product-index-0-0--0-100-1.json`(注意:这个URL可能需要根据实际情况调整,不同版本的API可能略有不同)但是,API调用需要先登录,并且后续请求需要携带登录后的Session(Cookie)。下面是一个C#示例,使用`HttpClient`进行登录并获取产品列表:```csharpusingSystem;usingSystem.Collections.Generic;usingSystem.Net.Http;usingSystem.Net.Http.Headers;usingSystem.Text;usingSystem.Threading.Tasks;namespaceZentaoIntegration{classProgram{staticasyncTaskMain(string[]args){varzentaoUrl="http://localhost:80";varusername="admin";varpassword="123456";//创建HttpClientHandler以允许自动处理Cookievarhandler=newHttpClientHandler{UseCookies=true,UseDefaultCredentials=false};using(varclient=newHttpClient(handler)){client.BaseAddress=newUri(zentaoUrl);client.DefaultRequestHeaders.Accept.Clear();client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));//登录请求varloginContent=newFormUrlEncodedContent(new[]{newKeyValuePair<string,string>("account",username),newKeyValuePair<string,string>("password",password),newKeyValuePair<string,string>("keepLogin","1")});//发送登录请求HttpResponseMessageloginResponse=awaitclient.PostAsync("/zentao/user-login.json",loginContent);if(!loginResponse.IsSuccessStatusCode){Console.WriteLine("登录失败");return;}//登录成功后,handler会自动保存Cookie(Session),后续请求会携带这个Cookie//获取产品列表HttpResponseMessageproductResponse=awaitclient.GetAsync("/zentao/product-index-0-0--0-100-1.json");if(productResponse.IsSuccessStatusCode){stringproductJson=awaitproductResponse.Content.ReadAsStringAsync();Console.WriteLine(productJson);}else{Console.WriteLine("获取产品列表失败");}}}}}```###注意事项:1.API调用需要确保在中开启了API调用(默认是开启的)。2.API路径和参数可能因版本不同而有所变化,请参考具体版本的API文档。3.上述示例中的URL路径(如`/zentao/product-index-0-0--0-100-1.json`)可能需要根据实际情况调整。的前端页面URL和APIURL有时是同一个,通过参数区分,但API返回JSON数据。4.如果部署在子目录下,需要调整BaseAddress和请求的URL。###常见问题及解决:-登录失败:检查账号密码,以及是否允许API登录。-获取产品列表返回404:检查版本和API路径,可能需要查看源码或文档确认正确的API路径。###集成中的其他考虑:-错误处理:网络错误、系统错误等。-安全性:不要在代码中硬编码账号密码,应使用配置或更安全的方式。-性能:合理使用异步和批处理。###API文档API文档可以在的安装目录中的`api`文件夹下找到(如`zentao\module\api`),或者查看的官方文档(如果有提供)。另外,专业版和旗舰版提供更完善的API文档和RESTfulAPI。###相关问题§§相关问题§§:1.如何在C#中处理API返回的JSON数据?2.如何使用C#通过API创建一个新的任务?3.API调用时遇到跨域问题如何解决?4.如何通过C#上传文件到(例如作为任务的附件)?</think>### 项目管理简介 (ZenTao)是一款开源的项目管理软件,专注于**敏捷开发全流程管理**,支持Scrum和瀑布模型。核心功能包括: 1. **产品管理**:需求池、版本规划 2. **项目管理**:任务分解、进度跟踪 3. **质量管理**:Bug跟踪、测试用例管理 4. **文档管理**:知识库共享 5. **看板视图**:可视化任务流 #### C#集成的典型场景 当需要在C#应用中与交互时(如自动创建Bug/任务),需通过其**REST API**实现。以下是关键步骤: --- ### 示例:C#调用API创建Bug ```csharp using System; using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading.Tasks; public class ZentaoService { private readonly HttpClient _httpClient; private const string BaseUrl = "http://your-zentao-site.com"; public ZentaoService(HttpClient httpClient) { _httpClient = httpClient; _httpClient.BaseAddress = new Uri(BaseUrl); } // 登录获取Session public async Task<string> LoginAsync(string account, string password) { var content = new StringContent( $"account={account}&password={password}", Encoding.UTF8, "application/x-www-form-urlencoded" ); var response = await _httpClient.PostAsync("/zentao/user-login.json", content); response.EnsureSuccessStatusCode(); // 使用Cookie保持会话,无需解析token return "SESSION_COOKIE_AUTOMATICALLY_HANDLED"; // HttpClient自动处理Cookie } // 创建Bug(需先登录) public async Task CreateBugAsync(string title, string steps, int productId) { var bugData = new { title, steps, product = productId, severity = 3 // 严重级别1-4 }; var json = JsonSerializer.Serialize(bugData); var content = new StringContent(json, Encoding.UTF8, "application/json"); // 注意:实际URL需根据版本调整 var response = await _httpClient.PostAsync("/zentao/bug-create.json", content); response.EnsureSuccessStatusCode(); var responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Bug创建成功: {responseBody}"); } } // 使用示例 public static async Task Main() { var handler = new HttpClientHandler { UseCookies = true }; using var httpClient = new HttpClient(handler); var zentao = new ZentaoService(httpClient); await zentao.LoginAsync("admin", "yourPassword"); await zentao.CreateBugAsync("支付接口超时", "1. 调用支付API\n2. 等待>30秒超时", 15); } ``` --- ### 关键注意事项 1. **认证方式**: - 默认使用**Cookie会话**(登录后自动维持) - 专业版支持Token认证(`Header: Token=<your_token>`) 2. **API路径**: - 基础路径:`/zentao/{module}-{method}.json` - 示例:创建Bug → `/zentao/bug-create.json` 3. **版本兼容性**: - 不同版本API可能有差异 - 需在`后台->API`中开启接口访问 4. **数据格式**: - 请求:`application/json` 或 `form-urlencoded` - 响应:JSON格式(含`status`和`data`字段) --- ### 常见问题解决 | 问题现象 | 原因 | 解决方案 | |----------|------|----------| | 401未授权 | Cookie失效 | 重新登录或检查会话有效期 | | 404找不到资源 | API路径错误 | 查看API文档确认路径格式 | | 500服务器错误 | 参数缺失/格式错误 | 使用`JsonSerializerOptions`严格序列化 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值