Kong Admin API for PHP: A Framework-Agnostic Client for Seamless Kong Gateway Management

Mary-Kate Olsen
Release: 2024-11-06 08:52:02
Original
245 people have browsed it

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.

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
Copy after login
Copy after login

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
Copy after login
Copy after login

Using Kong Admin API for PHP

Here are some examples of common tasks you can perform with this package:

  1. 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'
    );
    
    Copy after login
    Copy after login
  2. Create Connector and Client

    use CleaniqueCoders\KongAdminApi\Client;
    use CleaniqueCoders\KongAdminApi\Connector;
    
    $connector = new Connector($configuration);
    $client = new Client($connector);
    
    Copy after login
  3. 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);
    
    Copy after login
  • 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);
    
    Copy after login
  • Get a Service

    composer require cleaniquecoders/kong-admin-api
    
    Copy after login
    Copy after login
  • 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
    
    Copy after login
    Copy after login
  1. Add a Plugin You can add plugins, such as rate limiting, with custom configurations:
use CleaniqueCoders\KongAdminApi\Configuration;

$configuration = new Configuration(
    base: 'http://127.0.0.1:8000',
    uri: 'admin-api',
    apiKey: 'your-api-key',
    keyName: 'api-key'
);
Copy after login
Copy after login

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

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!