Home > Article > Backend Development > Kong Admin API for PHP: A Framework-Agnostic Client for Seamless Kong Gateway Management
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.
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:
Below is a guide to help you get started with Kong Admin API for PHP.
To install the package, run:
composer require cleaniquecoders/kong-admin-api
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.
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
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
composer require cleaniquecoders/kong-admin-api
Delete a Service
#!/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
use CleaniqueCoders\KongAdminApi\Configuration; $configuration = new Configuration( base: 'http://127.0.0.1:8000', uri: 'admin-api', apiKey: 'your-api-key', keyName: 'api-key' );
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.
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
The above is the detailed content of Kong Admin API for PHP: A Framework-Agnostic Client for Seamless Kong Gateway Management. For more information, please follow other related articles on the PHP Chinese website!