用 workerman 實(shí)現(xiàn)的web服務(wù)器,執(zhí)行 php 文件的方法如下:
function exec_php_file($file) {
\ob_start();
// Try to include php file.
try {
include $file;
} catch (\Exception $e) {
echo $e;
}
return \ob_get_clean();
}
現(xiàn)在被執(zhí)行的文件需要 header,請(qǐng)問(wèn)該如何實(shí)現(xiàn)呢?不要回答用 response,然后 connection send 這種答案了,我的這個(gè)文件里面沒(méi)有 connection 這個(gè)變量的,謝謝。
另外請(qǐng)問(wèn)一下站長(zhǎng),為和標(biāo)簽里面沒(méi)有workerman標(biāo)簽?都沒(méi)有適合我這個(gè)問(wèn)題的標(biāo)簽啊。
return \ob_get_clean();
改成
$headers = [];
return new Response(200, headers, \ob_get_clean());
這樣
謝謝,$_POST 這個(gè)問(wèn)題解決了,但是 header 這個(gè)不行啊,主要代碼如下:
if (pathinfo($path, PATHINFO_EXTENSION) === 'php' && file_exists($file)) {
$_POST = $request->post();
return $connection->send(exec_php_file($file));
} else {
$response->withFile($file);
}
$file 里面定義的 $header 變量沒(méi)法傳到 exec_php_file 函數(shù)里面去。
function exec_php_file($file) {
\ob_start();
// Try to include php file.
try {
$headers = include $file;
} catch (\Exception $e) {
echo $e;
}
return new Response(200, headers, \ob_get_clean());
}
file里返回一個(gè)header數(shù)組。
我改成這樣了,它不跳轉(zhuǎn)啊:
$_POST = $request->post();
$response->withBody(exec_php_file($file));
$response->withHeaders(['Location' => 'http://www.phpernote.com/']);
return $connection->send($response);
但是我打開(kāi)這個(gè)頁(yè)面看返回的 headers ,確實(shí)有 Location 的,但是就是沒(méi)跳轉(zhuǎn):
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 851
Content-Type: text/html;charset=utf-8
Location: http://www.phpernote.com/
Server: workerman
謝謝指點(diǎn),改成這樣,終于可以了,但是代碼寫的太難看了,哈哈,看來(lái)這個(gè)還是不適合做 http 服務(wù)器啊:
if (pathinfo($path, PATHINFO_EXTENSION) === 'php' && file_exists($file)) {
$_POST = $request->post();
$content = exec_php_file($file);
if (!empty($_SERVER['header']['Location'])) {
$response->withStatus(301);
$response->withHeaders(['Location' => $_SERVER['header']['Location']]);
}
$response->withBody($content);
return $connection->send($response);
} else {
$response->withFile($file);
}
它本身就是底層的,不懂機(jī)制底層機(jī)制不會(huì)抽象當(dāng)然亂了。
http直接用官網(wǎng)的webman就好了,和平時(shí)業(yè)務(wù)一樣的寫法,一點(diǎn)不亂
謝謝建議,主要是我在用 gatework 做一個(gè)即時(shí)通訊的東西,里面涉及到一點(diǎn)點(diǎn) http 請(qǐng)求相關(guān)的東西,不想再用 webman 這個(gè)重型框架搞了,就想直接用 workerman 解決試一下。