0

I wrote a script that communicates with a popular game, called kahoot. It logs in and answer's each question, and then echo's when it is finished. Here is it working on the command line. However, when I add this functionality to my website, when I run it it gives me a 504 gateway timeout, even though before the timeout it should have produced some output and echoed it to the page.

Here is the website code:

Full execution code:

<html>
  <head>
    <title>Kahoot bot</title>
  </head>
  <body>
<?php
$username = 'kahootbot28@gmail.com';
$password = 'botkahoot28';
$loginUrl = 'https://create.kahoot.it/rest/authenticate';
$kahootId = $_GET['quizid'];
$type = $_GET['what'];
if ($type == "bot") {
  $call = "~/www/reteps.tk/go/kahoot-auto " . $_GET['gamepin'] . " " . $_GET['username'] . " ";
  echo($call);
}
$pageUrl = 'https://create.kahoot.it/rest/kahoots/' . $kahootId;
$loginheader = array(); 
$loginheader[] = 'content-type: application/json';
$loginpost = new stdClass();
$loginpost->username = $username;
$loginpost->password = $password;
$loginpost->grant_type = "password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($loginpost));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER,$loginheader);
$store = curl_exec($ch);
curl_close($ch);
$token = json_decode($store,true)["access_token"];
//get questions
$quizheader = array(); 
$quizheader[] = 'authorization: ' . $token;
$options = array(
    'http' => array(
        'method'  => 'GET',
        'header'  => "Authorization: ".$token."\r\n"
    )
  );
$pairs = array(
     0 => "red",
     1 => "blue",
     2 => "yellow",
     3 => "green",
);
$context = stream_context_create($options);
$raw_result = file_get_contents($pageUrl, false, $context);
$result = json_decode($raw_result,true)["questions"];
echo("<a href='kahoot_bot'>back</a><br>");
foreach($result as $value) {
  if ($type == "text") {
    echo($value['question']."  ");
  }
  $choices = $value['choices'];
  for($i=0;$i<count($choices);$i++) {
    if ($choices[$i]['correct'] == true) {
      if ($type == "text") {
        echo($choices[$i]['answer']."<br>");
      } elseif ($type == "bot") {
        $call .= $i;
      } else {
        echo($pairs[$i].", ");
      }
      break 1;
    }
  }
}
if ($type == "bot") {
  $old_result = "";
  $handle = popen($call . " 2>&1", "r");
  $result = fread($handle, 2096);
  echo($result);
  while ((strpos($result, "end") !== false) != true) {
    $result = fread($handle, 2096);
    sleep(1); 
    if ($result != $old_result) {
      echo($result);
      $old_result = $result;
    }
  }
  pclose($handle);
}
?>
</body>
</html>
Peter S
  • 827
  • 1
  • 8
  • 24
  • What is your nginx config? And first test if a simple ` – Tarun Lalwani Oct 12 '17 at 12:39
  • Usually, for PHP output for the browser doesn't work exactly like it does for the shell, it would wait for the entire script to complete running and show the output all at once. You might need to adjust the PHP timeout values. Also, I would recommend **editing out** the actual credentials from the posted code – deepanshu223 Oct 12 '17 at 12:54
  • @TarunLalwani php is working for other parts of my website. – Peter S Oct 13 '17 at 00:32
  • @deepanshu223 is there a way so it displays it as it runs? – Peter S Oct 13 '17 at 00:33
  • You need to set `fastcgi_read_timeout` in nginx, then also set `max_execution_time = 300` in `fpm` php.ini. See this article for more details https://easyengine.io/tutorials/php/increase-script-execution-time/ – Tarun Lalwani Oct 13 '17 at 05:28

0 Answers0