0

I am trying to make a Telegram bot, and trying to send a Header 200 code right in the beginning of my file. When I run this code the headers_sent() is returning false at the end of the code. How do I fix this?

<?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));
?>
Matthew Bergwall
  • 340
  • 1
  • 12
  • is output buffering turned on in the php.ini file (or through a htaccess file)? That will cause php to wait until the end (or a call to flush) to send the output, including headers. – Jonathan Kuhn Feb 02 '17 at 00:17
  • have you tried using: `header("HTTP/1.1 200 OK");` – atoms Feb 02 '17 at 00:18
  • @JonathanKuhn I don't have access to PHP.ini. what's the name for that so i can do init set? – Matthew Bergwall Feb 02 '17 at 00:19
  • by the time ini_set runs, buffering will already be on. ini_set won't do anything. You could call [ob_get_level()](https://php.net/ob_get_level) to check if it is started and close it that many times. – Jonathan Kuhn Feb 02 '17 at 00:22
  • @JonathanKuhn How do i close it, can you post an answer with example code so i can mark it as correct to help others? – Matthew Bergwall Feb 02 '17 at 00:24
  • Possible duplicate of [ob\_get\_level() starts at level 1](http://stackoverflow.com/questions/3641598/ob-get-level-starts-at-level-1) – Jonathan Kuhn Feb 02 '17 at 00:25
  • Or http://stackoverflow.com/questions/7549347/php-flush-all-levels-of-output-buffering; or http://stackoverflow.com/questions/8882383/how-to-disable-output-buffering-in-php – Jonathan Kuhn Feb 02 '17 at 00:28
  • @JonathanKuhn do i put that before or after my header – Matthew Bergwall Feb 02 '17 at 00:29
  • Guys, it's interesting that you are discussing output buffering for a script that does not show the slightest bit of output creation at all. Anyone ever experiences headers being sent without `echo`? – Sven Feb 02 '17 at 00:31
  • @Sven so what's a solution to this? – Matthew Bergwall Feb 02 '17 at 00:32
  • I guess you have to improve your problem description. What are you doing, what are you expecting to happen, what did happen instead and how does it differ from your expectation, if not obvious. – Sven Feb 02 '17 at 00:36
  • It might also be possible to use [flush](https://php.net/flush) and/or [ob_flush](https://php.net/ob_flush) to force php to send all output now instead of possibly waiting. – Jonathan Kuhn Feb 02 '17 at 00:41
  • @Sven i'm expecting it to respond with a success code immediately. But all it's doing is waiting the 5 sec for the script to run before it sends t. – Matthew Bergwall Feb 02 '17 at 00:43
  • http://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response should provide plenty of solutions for "respond to the browser, then run in the background doing more stuff" – Sven Feb 02 '17 at 00:55

1 Answers1

0

You can't "fix" this. The function headers_sent() will return whether PHP has already sent the HTTP headers because the script also already sent some content (printing characters via echo, print or any other function that outputs something). This is for a script to determine if a call to headers() would be able to add more headers because they aren't sent.

The code you show does not output anything, so there is no need for PHP to send the headers. They will likely be sent after the script ends - at which point you'd be unable to resurrect the script in order to ask headers_sent() if the headers were now sent.

Sven
  • 69,403
  • 10
  • 107
  • 109