emptygif
言語: 日本語

すべての言語・フレームワーク

Slim PHP

/pixel.gif へのリクエストに空の GIF を返す、Slim 用の PHP スニペット。GIF は起動時に一度だけデコードします。

Slim PHP

インストール
composer require slim/slim slim/psr7
ファイル
index.php
実行
php -S 0.0.0.0:8080 index.php
<?php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/vendor/autoload.php';

$gif = base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');

$app = AppFactory::create();

$app->get('/pixel.gif', function (Request $request, Response $response) use ($gif) {
    $response->getBody()->write($gif);

    return $response
        ->withHeader('Content-Type', 'image/gif')
        ->withHeader('Content-Length', (string) strlen($gif))
        ->withHeader('Cache-Control', 'no-store');
});

$app->run();

このスニペットは実行し、返されたバイト列を照合しました。

確認

curl -sI http://localhost:8080/pixel.gif

公式サイト: Slim

PHP の他の例