I'm excited to introduce Kong Admin API for PHP, a powerful and framework-independent PHP package that simplifies interactions with Kong Gateway's Admin API. This package is a step up from our previous, Laravel-specific client (nasrulhazim/kong-gateway-php-client
), which will now be archived.
Our new package offers a versatile solution for developers across any PHP environment, providing flexibility to manage Kong Gateway with ease, regardless of framework.
Why Kong Admin API for PHP?
Kong Gateway’s flexibility, scalability, and features make it ideal for API management. However, the existing PHP clients were either tied to specific frameworks or lacked robust flexibility. This new package addresses these needs, providing:
- Framework-Agnostic Design: Compatible with any PHP framework or custom project.
- Comprehensive API Support: Effortlessly manage Kong resources with streamlined CRUD operations.
- Configurable Authentication: API key-based authentication is built-in, offering secure access management.
- Modular Setup: Customize the API’s base URL, headers, and more for your environment.
- Reliable Error Handling: Avoid downtime with robust error handling, ensuring stable API interactions.
Quickstart Guide
Below is a guide to help you get started with Kong Admin API for PHP.
Installation
To install the package, run:
composer require cleaniquecoders/kong-admin-api
Setting Up Kong on Docker
If you’re new to Kong or want to set it up quickly in a local development environment, check out our guide on Setting up Kong Gateway with Docker. This guide provides a step-by-step Docker setup for running Kong locally, ideal for testing and development.
Configuring Kong Gateway API Loopback
First, configure Kong to allow loopback connections on the Admin API, enabling secure, header-based authentication. Here’s an example of how to set it up with curl:
#!/bin/bash
# Create Admin API Service
curl --request POST --url http://localhost:8001/services --data name=admin-api-service --data url='http://localhost:8001'
# Create Admin API Route
curl --request POST --url http://localhost:8001/services/admin-api-service/routes --data 'paths[]=/admin-api' --data name=admin-api-route
# Enable Key Auth on Admin API Service
curl --request POST --url http://localhost:8001/services/admin-api-service/plugins --header 'Content-Type: application/json' --data '{"name":"key-auth","config":{"key_names":["api-key"],"key_in_query":false}}'
# Create Admin API Consumer
curl --request POST --url http://localhost:8001/consumers --data '{"username":"apim","custom_id":"apim"}'
# Create APIM API Key
curl -X POST http://localhost:8001/consumers/apim/key-auth
Using Kong Admin API for PHP
Here are some examples of common tasks you can perform with this package:
-
Initialize Configuration
use CleaniqueCoders\KongAdminApi\Configuration; $configuration = new Configuration( base: 'http://127.0.0.1:8000', uri: 'admin-api', apiKey: 'your-api-key', keyName: 'api-key' );
-
Create Connector and Client
use CleaniqueCoders\KongAdminApi\Client; use CleaniqueCoders\KongAdminApi\Connector; $connector = new Connector($configuration); $client = new Client($connector);
Example Operations
-
Store a New Service
use CleaniqueCoders\KongAdminApi\Enums\Endpoint; use CleaniqueCoders\KongAdminApi\Request; $response = $client->send( (new Request) ->setEndPoint(Endpoint::SERVICES) ->store(['name' => 'Some Service', 'url' => 'http://api.service.com']) ); print_r($response);
-
Update a Service
$response = $client->send( (new Request) ->setEndPoint(Endpoint::SERVICES) ->update('b3c12a56-1234-4f90-876d-12a5b678abcd', ['url' => 'http://new-example.com']) ); print_r($response);
-
Get a Service
$response = $client->send( (new Request) ->setEndPoint(Endpoint::SERVICES) ->get('b3c12a56-1234-4f90-876d-12a5b678abcd') ); print_r($response);
-
Delete a Service
$response = $client->send( (new Request) ->setEndPoint(Endpoint::SERVICES) ->delete('b3c12a56-1234-4f90-876d-12a5b678abcd') ); print_r($response);
- Add a Plugin You can add plugins, such as rate limiting, with custom configurations:
use CleaniqueCoders\KongAdminApi\Enums\Plugin;
$response = $client->send(
(new Request)
->plugin(Plugin::RATE_LIMITING, Endpoint::CONSUMERS, 'consumer-id')
->store(['config' => ['minute' => 5]])
);
print_r($response);
Need Help with Kong API Gateway?
Thinking about implementing Kong as your API gateway or need assistance optimizing your setup? We offer consultations on setting up and configuring Kong Gateway for seamless API management. You can schedule an appointment here: Book a consultation.
Conclusion
Kong Admin API for PHP provides a flexible and reliable solution for managing Kong Gateway’s API resources. With support for multiple frameworks and robust configuration options, it’s designed to integrate easily and empower developers with full control over Kong’s powerful features.
Photo by Codioful (Formerly Gradienta) on Unsplash
Top comments (0)