PHP code example of romchik38 / php-api

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

    

romchik38 / php-api example snippets


use Romchik38\Api\ErrorDto;

$dto = new ErrorDto('Resource not found');
echo json_encode($dto);
// {"status":"error","result":"Resource not found"}

use JsonSerializable;
use Romchik38\Api\SuccessDto;

class UserView implements JsonSerializable
{
    public function __construct(
        public readonly int $id,
        public readonly string $name,
    ) {}

    public function jsonSerialize(): mixed
    {
        return ['id' => $this->id, 'name' => $this->name];
    }
}

$dto = new SuccessDto(new UserView(1, 'Alice'));
echo json_encode($dto);
// {"status":"success","result":{"id":1,"name":"Alice"}}
bash
composer