m4tthumphrey/php-gitlab-api
Modern GitLab API v4 client for PHP 8.1–8.4. Provides a clean, php-github-api-inspired interface to GitLab endpoints, with PSR-18 HTTP client and PSR-17 factory support for flexible integration and authentication.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require m4tthumphrey/php-gitlab-api guzzlehttp/guzzle
For Laravel, use the dedicated package:
composer require graham-campbell/gitlab
Basic Authentication:
use Gitlab\Client;
$client = new Client();
$client->authenticate(env('GITLAB_TOKEN'), Client::AUTH_HTTP_TOKEN);
First Use Case: Fetch a project by ID:
$project = $client->projects()->show(123);
new Gitlab\Client() (supports custom HTTP clients via Gitlab\HttpClient\Builder).$client->projects(), $client->issues()).Gitlab\ResultPager for paginated endpoints (e.g., $client->issues()->all()).$project = $client->projects()->create('My Project', [
'description' => 'Test project',
'visibility' => 'private',
]);
$projects = $client->projects()->all(['search' => 'backend']);
$pipeline = $client->projects()->createPipeline(123, [
'ref' => 'main',
'variables' => [['key' => 'ENV', 'value' => 'production']],
]);
$jobs = $client->projects()->pipelineJobs(123, 456); // project_id, pipeline_id
$mr = $client->mergeRequests()->create(123, [
'source_branch' => 'feature-branch',
'target_branch' => 'main',
'title' => 'Fix bug',
]);
$pager = new Gitlab\ResultPager($client);
$mrs = $pager->fetchAll($client->mergeRequests(), 'all', [123, ['state' => 'opened']]);
$issue = $client->issues()->create(123, [
'title' => 'Bug report',
'description' => 'Details here...',
]);
$issues = $client->issues()->all(123, [
'state' => 'closed',
'author_id' => 789,
]);
$hook = $client->projects()->addHook(123, [
'url' => 'https://example.com/webhook',
'push_events' => true,
]);
Service Provider Binding:
// config/services.php
'gitlab' => [
'token' => env('GITLAB_TOKEN'),
'url' => env('GITLAB_URL', 'https://gitlab.com'),
];
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(Gitlab\Client::class, function ($app) {
$client = new Gitlab\Client();
$client->setUrl(config('services.gitlab.url'));
$client->authenticate(config('services.gitlab.token'), Gitlab\Client::AUTH_HTTP_TOKEN);
return $client;
});
}
Facade Usage:
use GrahamCampbell\GitLab\Facades\GitLab;
$project = GitLab::projects()->show(123);
$plugin = new Http\Client\Common\Plugin\HeaderSetPlugin([
'User-Agent' => 'MyApp/1.0',
]);
$builder = new Gitlab\HttpClient\Builder();
$builder->addPlugin($plugin);
$client = new Gitlab\Client($builder);
try {
$client->projects()->show(123);
} catch (Gitlab\Exception\GitlabException $e) {
Log::error('GitLab API Error: ' . $e->getMessage());
// Handle specific error codes (e.g., 404 for missing project)
}
Authentication:
Gitlab\Client::AUTH_JOB_TOKEN for CI/CD job-specific access (e.g., $client->authenticate($CI_JOB_TOKEN, Client::AUTH_JOB_TOKEN)).Pagination:
recent()) require explicit pagination via ResultPager:
$pager = new Gitlab\ResultPager($client);
$items = $pager->fetchAll($client->projects(), 'recent');
Parameter Handling:
['created_after' => '2023-01-01T00:00:00.000Z']).Rate Limiting:
use Http\Client\Common\Plugin\RetryPlugin;
$retryPlugin = new RetryPlugin();
$builder->addPlugin($retryPlugin);
Self-Hosted GitLab:
$client->setUrl('https://git.example.com');
gitlab.com. Check your instance’s API docs.Enable Debugging:
$stack = Http\Message\MultipartStream\ServerRequestFactory::createStack();
$stack->pushMiddleware(new Http\Client\Common\Plugin\LogPlugin(null, true));
$builder = new Gitlab\HttpClient\Builder();
$builder->setHandler($stack);
Common HTTP Errors:
Deprecation Warnings:
Custom API Endpoints:
use Gitlab\Client;
use Gitlab\Api;
class CustomApi extends Api
{
public function customEndpoint($param)
{
return $this->get("api/v4/custom", ['param' => $param]);
}
}
$client->setApi(new CustomApi($client));
Middleware:
$plugin = new class implements Http\Client\Plugin {
public function __invoke(Callable $handler) {
return function ($request) use ($handler) {
// Modify request
$request = $request->withHeader('X-Custom-Header', 'value');
return $handler($request);
};
}
};
$builder->addPlugin($plugin);
Caching:
php-http/cache-plugin for caching responses:
$cachePlugin = new Http\Cache\Plugin($cachePool);
$builder->addPlugin($cachePlugin);
Testing:
Gitlab\Client and Http\Mock\Client:
$mock = new Http\Mock
How can I help you explore Laravel packages today?