A simple PHP Redmine API client, Object Oriented
A simple PHP Object Oriented wrapper for Redmine API.
Uses Redmine API.
cURL
function or any
PSR-18 HTTP client implementation
like Guzzle for handling http connections$client->getApi('issue')->create(['project_id' => 1, 'subject' => 'issue title']);
$client->requestPost('/issues.json', '{"issue":{"project_id":1,"subject":"issue title"}}');
We support (and run tests against) the latest Redmine versions that are actively maintained.
Nevertheless, you can also use this library for all older Redmine versions. In this case, however, be aware that some features may not yet be supported by your Redmine server.
If a new Redmine version enables new features that are not yet supported with this library, you are welcome to create an issue.
/settings/edit?tab=api
) and check the "Enable REST web service" box/my/account
cURL
functions.Redmine is missing some APIs for a full remote management of the data:
By using Composer you can simply run:
$ php composer.phar require kbsali/redmine-api
at the root of your projects. To utilize the library, include
Composer's vendor/autoload.php
in the scripts that will use the
Redmine
classes.
For example,
<?php
// This file is generated by Composer
require_once 'vendor/autoload.php';
$client = new \Redmine\Client\NativeCurlClient('https://redmine.example.com', '0ef19567656532f8dd43a4dbfeda787f01f3e659');
For a manual installation please follow this instruction.
You can run test suite to make sure the library will work properly on your system. Simply run vendor/bin/phpunit
in the project's directory :
$ 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
clientCreate your project e.g. in the index.php
by require the vendor/autoload.php
file.
+<?php
+
+require_once 'vendor/autoload.php';
You can choose between:
Redmine\Client\NativeCurlClient
:bulb: This client was introduced in
php-redmine-api
v1.8.0. If you are using the oldRedmine\Client
please see this migration guide for help to upgrade your code.
You will need a URL to your Redmine instance and either a valid Apikey...
<?php
require_once 'vendor/autoload.php';
+
+// Instantiate with ApiKey
+$client = new \Redmine\Client\NativeCurlClient('https://redmine.example.com', '1234567890abcdfgh');
... or valid username/password.
<?php
require_once 'vendor/autoload.php';
+
+// Instantiate with Username/Password (not recommended)
+$client = new \Redmine\Client\NativeCurlClient('https://redmine.example.com', 'username', 'password');
:bulb: For security reason it is recommended that you use an ApiKey rather than your username/password.
After you instantiate a client you can set some optional cURL
settings.
<?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']);
Redmine\Client\Psr18Client
:bulb: This client was introduced in
v1.7.0
of this library. If you are using the oldRedmine\Client
please follow this migration guide.
The Psr18Client
requires
Psr\Http\Client\ClientInterface
implementation (like guzzlehttp/guzzle), see
Psr\Http\Message\RequestFactoryInterface
implementation (like guzzlehttp/psr7), see
Psr\Http\Message\StreamFactoryInterface
implementation (like guzzlehttp/psr7), see
:bulb: For security reason it is recommended that you use an ApiKey rather than your username/password.
<?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'
+);
Because the Psr18Client
is agnostic about the HTTP client implementation every configuration specific to the transport has to be set to the Psr\Http\Client\ClientInterface
implementation.
This means that if you want to set any cURL
settings to Guzzle
you have multiple ways to set them:
Psr\Http\Client\ClientInterface
wrapper:<?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 allows you to impersonate another user. This can be done using the methods startImpersonateUser()
and stopImpersonateUser()
.
$client->startImpersonateUser('kim');
// all requests will now impersonate the user `kim`
// To stop impersonation
$client->stopImpersonateUser();
You can now use the getApi()
method to create and get a specific Redmine API.
<?php
$client->getApi('user')->list();
$client->getApi('user')->listing();
$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
]);
See further examples and read more about usage in the docs.