PHP code example of tugrul / apigen

1. Go to this page and download the library: Download tugrul/apigen library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tugrul / apigen example snippets


// 1. Define
interface UserApi {
    #[GET('/users/{id}')]
    #[Returns('array')]
    public function getUser(#[Path] int $id): array;

    #[DELETE('/users/{id}')]
    public function deleteUser(#[Path] int $id): void;
}

// 2. Generate (once, at development time)
$gen = new StubGenerator(__DIR__ . '/Generated');
$gen->generate(UserApi::class);

// 3. Use
$client = ClientBuilder::create('https://api.example.com')
    ->withPsr18($httpClient, $psr17, $psr17, $psr17)
    ->withBearerToken('my-token')
    ->build();

$api  = new UserApiImpl($client);
$user = $api->getUser(42);
$api->deleteUser(42);