emptygif
言語: 日本語

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

ReactPHP PHP

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

ReactPHP PHP

インストール
composer require react/http
ファイル
server.php
実行
php server.php
<?php

use Psr\Http\Message\ServerRequestInterface;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Socket\SocketServer;

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

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

$http = new HttpServer(function (ServerRequestInterface $request) use ($gif) {
    if ($request->getMethod() !== 'GET' || $request->getUri()->getPath() !== '/pixel.gif') {
        return new Response(Response::STATUS_NOT_FOUND);
    }

    return new Response(Response::STATUS_OK, [
        'Content-Type' => 'image/gif',
        'Cache-Control' => 'no-store',
    ], $gif);
});

$http->listen(new SocketServer('0.0.0.0:8080'));

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

確認

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

公式サイト: ReactPHP

PHP の他の例