【问题标题】:Header Success Code PHP标头成功代码 PHP
【发布时间】: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 我如何关闭它,你能用示例代码发布答案,以便我可以将其标记为正确以帮助他人吗?

标签: php header telegram


【解决方案1】:

您无法“修复”此问题。 headers_sent() 函数将返回 PHP 是否已经发送了 HTTP 标头,因为脚本也已经发送了一些内容(通过 echoprint 或任何其他输出内容的函数打印字符)。这是为了让脚本确定对headers() 的调用是否能够添加更多标头,因为它们没有被发送。

您显示的代码不输出任何内容,因此 PHP 无需发送标头。它们可能会在脚本结束后发送 - 此时您将无法恢复脚本以询问 headers_sent() 是否现在发送了标头。

【讨论】:

    猜你喜欢
    • 2012-12-29
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2021-08-09
    • 1970-01-01
    相关资源
    最近更新 更多