【发布时间】:2017-02-02 00:14:03
【问题描述】:
我正在尝试制作一个 Telegram 机器人,并尝试在我的文件开头发送一个 Header 200 代码。当我运行此代码时,headers_sent() 在代码末尾返回 false。我该如何解决这个问题?
<?php
http_response_code(200);
set_time_limit(20000);
$botToken = "259050740:AAHRRa8zLKwKmRhY_W3u0YlEYovbLEumCZM";
$website = "https://api.telegram.org/bot" . $botToken;
$update = file_get_contents("php://input");
$updateArray = json_decode($update, TRUE);
$SenderID = $updateArray['message']['chat']['id'];
$Message = $updateArray['message']['text'];
$updateId = $updateArray['update_id'];
/*$ids = explode("\n",file_get_contents("used.txt"));
foreach($ids as $id) {
if($id == $updateId) {
exit(0);
}
}
$fp = fopen("used.txt", 'a');
fwrite($fp, $updateId."\n");
fclose($fp);*/
//file_get_contents($website . "/sendmessage?chat_id=" . $SenderID . "&text=1%0A%0A".urlencode($update));
sleep(5);
//file_get_contents($website . "/sendmessage?chat_id=" . $SenderID . "&text=2%0A%0A".urlencode($update));
?>
【问题讨论】:
-
是否在 php.ini 文件(或通过 htaccess 文件)中打开了输出缓冲?这将导致 php 等到结束(或调用刷新)发送输出,包括标题。
-
你试过用:
header("HTTP/1.1 200 OK"); -
@JonathanKuhn 我无权访问 PHP.ini。那叫什么名字,所以我可以做初始化设置?
-
在 ini_set 运行时,缓冲已经开启。 ini_set 不会做任何事情。您可以拨打ob_get_level() 来检查它是否已启动并多次关闭它。
-
@JonathanKuhn 我如何关闭它,你能用示例代码发布答案,以便我可以将其标记为正确以帮助他人吗?