PHP code example of utopia-php / openapi

1. Go to this page and download the library: Download utopia-php/openapi 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/ */

    

utopia-php / openapi example snippets


use Utopia\OpenAPI\Parser;

$json = file_get_contents('openapi.json');
$specification = Parser::parse($json);

$specification = Parser::parse([
    'openapi' => '3.1.0',
    'info' => [
        'title' => 'Example API',
        'version' => '1.0.0',
    ],
    'paths' => [],
]);

use Utopia\OpenAPI\Parser;
use Utopia\OpenAPI\Version;

$specification = Parser::parse(
    input: $json,
    version: Version::V3_1,
);

$parser = new Parser();
$specification = $parser->read($json);

use Utopia\OpenAPI\Model\HttpMethod;

foreach ($specification->paths as $path => $pathItem) {
    foreach ($pathItem->operations as $operation) {
        echo strtoupper($operation->method->value);
        echo ' ' . $operation->path . PHP_EOL;
    }
}

$getPet = $specification
    ->paths['/pets/{id}']
    ->operation(HttpMethod::GET);

$operations = $specification->operations();

$petOperations = $specification->operationsByTag('pets');

use Utopia\OpenAPI\Model\ReferenceSchema;

$schema = $specification->schemas['Pet'];

if ($schema instanceof ReferenceSchema) {
    echo $schema->reference;
}

use Utopia\OpenAPI\Model\CompositeSchema;
use Utopia\OpenAPI\Model\StringSchema;

if ($schema instanceof CompositeSchema) {
    $enum = $schema->stringEnum();
    $values = $enum?->enum ?? [];
    $keys = $enum?->enumKeys ?? [];
    $name = $enum?->enumName;
}

$owner = $specification->info->extensions['x-owner'] ?? null;

use Utopia\OpenAPI\Exception\OpenAPIException;
use Utopia\OpenAPI\Parser;

try {
    $specification = Parser::parse($json);
} catch (OpenAPIException $exception) {
    echo $exception->getMessage();
}