我试图让 Slim 在我的 Windows 7 系统上运行。到目前为止,我已经用 Composer 安装了所有东西,但是当我运行一个非常简单的程序时,输出并不像预期的那样。
下面是我的代码:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;
require '../vendor/autoload.php';
$app = new App;
$app->get('/', function (Request $in, Response $out, $args) {
return $out->write("xxxxx");
});
$app->run();
我期待输出“xxxxx”,而不是我得到“x”。
这意味着我在某处丢失了 4 个字符。运行 PHP 5.5.12 编码是 UTF-8(不是 BOM)
当我运行“curl -v http://localhost:8080/ ”
我明白了
D:\wamp\www\slim_test\src\public>curl -v http://localhost:8080/
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.46.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Host: localhost:8080
< Connection: close
< X-Powered-By: PHP/5.5.12
< Content-Type: text/html; charset=UTF-8
< Content-Length: 5
<
x* Closing connection 0
我会很感激你的帮助。
编辑 一旦我将这些代码行附加到文件的末尾,响应是正确的。
$response = $app->run(true); //Silent mode, wont send the response
$response = $response->withoutHeader("Content-Length"); //Remove the Content-Length
$app->respond($response); //Now we send the response
我想不通为什么……?