是否可以在 PHP CLI 服务器中启用 CORS?

Is it possible to enable CORS in the PHP CLI server?

是否可以在 PHP CLI server 中启用 CORS(如果可以,如何启用)?

编辑:为了解决诸如我应该在我的脚本中包含 header 之类的评论,请注意我的代码中没有任何 PHP files/scripts。我只是将 PHP CLI 服务器用作轻量级本地托管选项。因此,理想情况下,答案将提供 CLI 选项,或显示存在 none.

此功能未在内部网络服务器中实现。 Web 服务器仅用于基本测试,不用于生产。请注意 documentation:

顶部的 红色

Warning

This web server was designed to aid application development. It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. It is not intended to be a full-featured web server. It should not be used on a public network.

使用 php 路由脚本在 webhook 文件夹中使用 DocumentRoot 启动服务器:

php -S localhost:8888 -t webhook webhook/dev-routings.php

webhook/dev-routings.php :

<?php
// Copyright Monwoo 2017, service@monwoo.com
// Enabling CORS in bultin dev to test locally with multiples servers
// used to replace lack of .htaccess support inside php builting webserver.
// call with :
// php -S localhost:8888 -t webhook webhook/dev-routings.php
$CORS_ORIGIN_ALLOWED = "http://localhost:3000";  // or '*' for all   

function consoleLog($level, $msg) {
    file_put_contents("php://stdout", "[" . $level . "] " . $msg . "\n");
}

function applyCorsHeaders($origin) {
    header("Access-Control-Allow-Origin: $origin");
    header("Access-Control-Allow-Credentials: true");
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type, Accept');
}

if (preg_match('/\.(?:png|jpg|jpeg|gif|csv)$/', $_SERVER["REQUEST_URI"])) {
    consoleLog('info', "Transparent routing for : " . $_SERVER["REQUEST_URI"]);
    return false;
} else if (preg_match('/^.*$/i', $_SERVER["REQUEST_URI"])) {
    $filePath = "{$_SERVER['DOCUMENT_ROOT']}/{$_SERVER["REQUEST_URI"]}";
    applyCorsHeaders($CORS_ORIGIN_ALLOWED);

    if (!file_exists($filePath)) {
        consoleLog('info', "File not found Error for : " . $_SERVER["REQUEST_URI"]);
        // return false;
        http_response_code(404);
        echo "File not Found : {$filePath}";
        return true;
    }
    $mime = mime_content_type($filePath);
    // 
    // 
    // Otherwise, you can use custom rules :
    $customMappings = [
        'js' => 'text/javascript', //'application/javascript',
        'css' => 'text/css',
    ];
    $ext = pathinfo($filePath, PATHINFO_EXTENSION);
    // consoleLog('Debug', $ext);
    if (array_key_exists($ext, $customMappings)) {
        $mime = $customMappings[$ext];
    }
    consoleLog('info', "CORS added to file {$mime} : {$filePath}");
    header("Content-type: {$mime}");
    echo file_get_contents($filePath);
    return true;
} else {
    consoleLog('info', "Not catched by routing, Transparent serving for : "
    . $_SERVER["REQUEST_URI"]);
    return false; // Let php bultin server serve
}

对于那些仍在为此摸不着头脑的人,我遇到了同样的问题并解决了。对我来说,从 Webpack 开发服务器代理到我的 PHP 开发服务器没有用。

当您使用 localhost 作为服务器时,PHP 内置服务器似乎有 CORS 问题。相反,您应该使用您的本地 IP 地址,或者只是 127.0.0.1,它再次指向您的本地计算机。所以,你会像这样启动你的服务器:

php -S 127.0.0.1:8888 -t public

现在 Webpack Dev Server 代理可以工作了。您也可以使用 0.0.0.0。希望这可以帮助。

来自 google 并使用 built-in-php-server 的人:放在 file-to-be-served.php 的第一行:

header("Access-Control-Allow-Origin: *");

取自: https://enable-cors.org/server_php.html