emptygif
ภาษา: ไทย

ภาษาและเฟรมเวิร์กทั้งหมด

FrankenPHP (worker mode) PHP

ตัวอย่างโค้ด PHP สำหรับ FrankenPHP (worker mode) ที่ตอบคำขอไปยัง /pixel.gif ด้วย GIF ว่าง ซึ่งถอดรหัสเพียงครั้งเดียวตอนเริ่มทำงาน

FrankenPHP (worker mode) PHP

ไฟล์
index.php
รัน
frankenphp php-server --listen :8080 --worker index.php
<?php

// Worker script: booted once, then handles requests in a loop.
ignore_user_abort(true);

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

$handler = static function () use ($gif): void {
    if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) !== '/pixel.gif') {
        http_response_code(404);
        return;
    }
    header('Content-Type: image/gif');
    header('Content-Length: ' . strlen($gif));
    header('Cache-Control: no-store');
    echo $gif;
};

while (frankenphp_handle_request($handler)) {
    gc_collect_cycles();
}

เรารันตัวอย่างโค้ดนี้และเทียบไบต์ที่ได้กลับมาแล้ว

ตรวจสอบ

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

เว็บไซต์ทางการ: FrankenPHP (worker mode)

เพิ่มเติมใน PHP