cache = $cache; } public function sendCode(Chat $chat, $code) { $params = [ "log" => 'on', "mes" => '', "legend" => 'on', "nostat" => '', "notext" => '', "refresh" => '0', "bonus" => '', "kladMap" => '', "notags" => '', "cod" => $code, "action" => 'entcod', ]; $data = http_build_query($params); $contextData = [ 'http' => [ 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $data ] ]; $context = stream_context_create($contextData); $result = file_get_contents($this->getLink($chat), false, $context); $contextData['result'] = $result; file_put_contents(getenv('OPENSHIFT_DATA_DIR') . 'log.txt', serialize($contextData)); return $this->parseResult($result); } private function parseResult($html) { $crawler = new Crawler($html); $answer = [ $this->parseCodeEnter($html), $this->parseTask($crawler) ]; $answer = array_merge($answer, $this->parseCodes($crawler)); $answer = array_filter($answer); return implode(PHP_EOL, $answer); } private function parseCodeEnter($html) { $html = iconv('cp1251', 'utf8', $html); $regex = '/\<\!\-\-errorText\-\-\>\\(.+?)\<\/strong\>\<\/p\>\<\!\-\-errorTextEnd\-\-\>'; $result = []; preg_match($regex, $html, $result); return $result[1]; } private function parseTask(Crawler $crawler) { return $crawler->filter('body > p')->reduce(function ($node) { return strpos($node->text(), 'Задание '); })->text(); } private function parseCodes(Crawler $crawler) { $codesHtml = $crawler->filter('body > .dcodes')->html(); $codesHtml = preg_replace('/\(.+?)\<\/span\>/i', '*[$1]*', $codesHtml); $codesHtml = preg_replace('/\/i', PHP_EOL, $codesHtml); $codesHtml = strip_tags($codesHtml); $codesHtml = explode(PHP_EOL, $codesHtml); array_shift($codesHtml); return $codesHtml; } private function getLink(Chat $chat) { return $this->cache->fetch("link_{$chat->getId()}"); } }