kbsali / redmine-api
Redmine API客户端
Requires
- php: ^7.4 || ^8.0
- ext-curl: *
- ext-json: *
- ext-simplexml: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
Requires (Dev)
- behat/behat: ^3.14
- friendsofphp/php-cs-fixer: ^3.45
- guzzlehttp/psr7: ^2
- php-mock/php-mock-phpunit: ^2.6
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.5 || ^10.5
- v2.x-dev
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- v1.x-dev
- v1.8.1
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.22
- v1.5.21
- v1.5.20
- v1.5.19
- v1.5.18
- v1.5.16
- v1.5.15
- v1.5.14
- v1.5.13
- v1.5.12
- v1.5.11
- v1.5.10
- v1.5.9
- v1.5.8
- v1.5.7
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4
- v1.3
- v1.2.1
- v1.2
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.2
- 1.0.0
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2
- 0.1
- dev-renovate/all
This package is auto-updated.
Last update: 2024-09-10 12:15:33 UTC
README
Redmine API的一个简单的PHP面向对象封装。
使用Redmine API。
功能
- 遵循PSR-4约定和编码标准:自动加载友好
- 选择使用原生
cURL
函数或任何PSR-18 HTTP客户端实现(如Guzzle)来处理HTTP连接 - 中级别API例如。
$client->getApi('issue')->create(['project_id' => 1, 'subject' => 'issue title']);
- 低级别API例如。
$client->requestPost('/issues.json', '{"issue":{"project_id":1,"subject":"issue title"}}');
支持的Redmine版本
我们支持(并对以下版本进行测试)接收安全更新的最新支持的Redmine版本。
- Redmine 5.1.x
- Redmine 5.0.x
- Redmine 4.2.x
尽管如此,您也可以使用此库的所有旧版Redmine。但是,请注意,您的Redmine服务器可能不支持某些功能。
如果新的Redmine版本启用了此库尚不支持的新功能,您可以通过创建问题。
要求
- PHP ^7.4 || ^8.0
- PHP SimpleXML 扩展
- PHP JSON 扩展
- 在您的Redmine服务器上启用了REST web服务
- 转到管理 -> 设置 -> Api (
/settings/edit?tab=api
) 并勾选“启用REST web服务”框 - 在您的个人资料页面中获取您的API访问密钥:
/my/account
- (或使用您的用户名 & 密码;不推荐)
- 转到管理 -> 设置 -> Api (
可选
待办事项
- 在#305中跟踪Redmine API功能支持
- 检查响应头中的状态码(特别是对于POST/PUT/DELETE请求)
局限性 / 缺失的Redmine-API
Redmine缺少一些API,无法完全远程管理数据
安装
使用Composer,您可以在项目根目录中简单地运行
$ php composer.phar require kbsali/redmine-api
要使用此库,请将Composer的vendor/autoload.php
包含到将使用Redmine
类的脚本中。
例如,
<?php // This file is generated by Composer require_once 'vendor/autoload.php'; $client = new \Redmine\Client\NativeCurlClient('https://redmine.example.com', '0ef19567656532f8dd43a4dbfeda787f01f3e659');
请按照以下说明进行手动安装:说明链接。
运行测试套件
您可以通过运行项目目录下的vendor/bin/phpunit
来运行测试套件,以确保库在您的系统上可以正常工作。
$ vendor/bin/phpunit
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.
Warning: No code coverage driver available
............................................................... 63 / 432 ( 14%)
............................................................... 126 / 432 ( 29%)
............................................................... 189 / 432 ( 43%)
............................................................... 252 / 432 ( 58%)
............................................................... 315 / 432 ( 72%)
............................................................... 378 / 432 ( 87%)
...................................................... 432 / 432 (100%)
Time: 00:00.149, Memory: 14.00 MB
OK (432 tests, 1098 assertions)
php-redmine-api
客户端的基本用法
开始您的项目
创建您的项目,例如在index.php
中通过引入vendor/autoload.php
文件来实现。
+<?php + +require_once 'vendor/autoload.php';
实例化Redmine客户端
您可以选择以下之一:
- 本机curl客户端或
- PSR-18兼容客户端。
1. 本机curl客户端 Redmine\Client\NativeCurlClient
💡 该客户端自
php-redmine-api
v1.8.0版引入。如果您正在使用旧的Redmine\Client
,请参阅此迁移指南以帮助升级您的代码。
您需要一个指向您的Redmine实例的URL以及一个有效的Apikey...
<?php require_once 'vendor/autoload.php'; + +// Instantiate with ApiKey +$client = new \Redmine\Client\NativeCurlClient('https://redmine.example.com', '1234567890abcdfgh');
... 或者有效的用户名/密码。
<?php require_once 'vendor/autoload.php'; + +// Instantiate with Username/Password (not recommended) +$client = new \Redmine\Client\NativeCurlClient('https://redmine.example.com', 'username', 'password');
💡 由于安全原因,建议您使用ApiKey而不是用户名/密码。
cURL配置
实例化客户端后,您可以设置一些可选的cURL
设置。
<?php require_once 'vendor/autoload.php'; // Instantiate with ApiKey $client = new Redmine\Client\NativeCurlClient('https://redmine.example.com', '1234567890abcdfgh'); + +// [OPTIONAL] if you want to check the servers' SSL certificate on Curl call +$client->setCurlOption(CURLOPT_SSL_VERIFYPEER, true); + +// [OPTIONAL] set the port (it will try to guess it from the url) +$client->setCurlOption(CURLOPT_PORT, 8080); + +// [OPTIONAL] set a custom host +$client->setCurlOption(CURLOPT_HTTPHEADER, ['Host: https://custom.example.com']);
2. PSR-18兼容客户端 Redmine\Client\Psr18Client
💡 该客户端自该库的v1.7.0版引入。如果您正在使用旧的
Redmine\Client
,请遵循此迁移指南。
Psr18Client
需要
- 一个
Psr\Http\Client\ClientInterface
实现(如guzzlehttp/guzzle),查看 - 一个
Psr\Http\Message\RequestFactoryInterface
实现(如guzzlehttp/psr7),查看 - 一个
Psr\Http\Message\StreamFactoryInterface
实现(如guzzlehttp/psr7),查看 - 指向您的Redmine实例的URL
- 一个Apikey或用户名
- 以及可选的密码(如果您想使用用户名/密码)。
💡 由于安全原因,建议您使用ApiKey而不是用户名/密码。
<?php require_once 'vendor/autoload.php'; + +$guzzle = new \GuzzleHttp\Client(); +$psr17Factory = new \GuzzleHttp\Psr7\HttpFactory(); + +// Instantiate with ApiKey +$client = new \Redmine\Client\Psr18Client( + $guzzle, + $psr17Factory, + $psr17Factory, + 'https://redmine.example.com', + '1234567890abcdfgh' +); +// ...or Instantiate with Username/Password (not recommended) +$client = new \Redmine\Client\Psr18Client( + $guzzle, + $psr17Factory, + $psr17Factory, + 'https://redmine.example.com', + 'username', + 'password' +);
Guzzle配置
由于Psr18Client
对HTTP客户端实现是中立的,因此针对传输的特定配置必须设置到Psr\Http\Client\ClientInterface
实现中。
这意味着,如果您想将任何cURL
设置设置到Guzzle
,您有多种方式可以设置它们
- 使用Guzzle环境变量
- 使用请求选项在
Psr\Http\Client\ClientInterface
包装器内
<?php require_once 'vendor/autoload.php'; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; + $guzzle = \GuzzleHttp\Client(); $psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory(); +$guzzleWrapper = new class(\GuzzleHttp\Client $guzzle) implements ClientInterface +{ + private $guzzle; + + public function __construct(\GuzzleHttp\Client $guzzle) + { + $this->guzzle = $guzzle; + } + + public function sendRequest(RequestInterface $request): ResponseInterface + { + return $this->guzzle->send($request, [ + // Set the options for every request here + 'auth' => ['username', 'password', 'digest'], + 'cert' => ['/path/server.pem', 'password'], + 'connect_timeout' => 3.14, + // Set specific CURL options, see https://docs.guzzlephp.org/en/stable/faq.html#how-can-i-add-custom-curl-options + 'curl' => [ + CURLOPT_SSL_VERIFYPEER => 1, + CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, + ], + ]); + } +}; + // Instantiate with ApiKey $client = new \Redmine\Client\Psr18Client( - $guzzle, + $guzzleWrapper, $psr17Factory, $psr17Factory, 'https://redmine.example.com', '1234567890abcdfgh' );
内置的Redmine功能
模拟用户
Redmine允许您使用startImpersonateUser()
和stopImpersonateUser()
方法模拟其他用户。这可以通过使用以下方法来完成:
$client->startImpersonateUser('kim'); // all requests will now impersonate the user `kim` // To stop impersonation $client->stopImpersonateUser();
API使用
您现在可以使用getApi()
方法创建和获取特定的Redmine API。
<?php $client->getApi('user')->list(); $client->getApi('user')->listLogins(); $client->getApi('issue')->create([ 'project_id' => 'test', 'subject' => 'some subject', 'description' => 'a long description blablabla', 'assigned_to_id' => 123, // or 'assigned_to' => 'user1' OR 'groupXX' ]); $client->getApi('issue')->list([ 'limit' => 1000 ]);
感谢!
- 感谢Thomas Spycher为类的第一个版本做出贡献。
- 感谢Thibault Duplessis aka. ornicar为php-github-api库做出贡献,这是一个灵感之源!
- 以及所有贡献者
- 特别感谢JanMalte对测试覆盖率的巨大贡献! :)