Advertisement
fudo

baza-otvetov.ru/quiz - PARSER

May 31st, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.66 KB | None | 0 0
  1. <?php
  2. header('Content-Type: text/json; charset=utf-8');
  3. $opts = array(
  4.   'http'=>array(
  5.     'method'=>"GET",
  6.     'header'=>"x-requested-with: XMLHttpRequest\r\n"
  7.   )
  8. );
  9. $context = stream_context_create($opts);
  10. $file = file_get_contents('https://baza-otvetov.ru/quiz/ask', false, $context);
  11. $content = explode("</script>", $file)[1];
  12. $p = xml_parser_create();
  13.      xml_parse_into_struct($p, $content, $vals, $index);
  14.      xml_parser_free($p);
  15. $id = $vals[0]["attributes"]["ID"];
  16. $question = $vals[0]["value"];
  17. $othercont = explode("</table>", explode("<table border='0' cellspacing='10'>", $content)[1])[0];
  18. $doc = new DOMDocument();
  19. $doc->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . $othercont);
  20. $tags = $doc->getElementsByTagName('h4');
  21. $_val = "";
  22. $vals = array();
  23. for ($i = 0; $i < $tags->length; $i++) {
  24.     $val = $tags->item($i)->nodeValue;
  25.     $data = array(
  26.         'q_id' => $id,
  27.         'answer' => $val
  28.     );
  29.     $postString = http_build_query($data, '', '&');
  30.     $opts = array('http' =>
  31.         array(
  32.             'method'  => 'POST',
  33.             'header'  => "Content-type: application/x-www-form-urlencoded\r\n" .
  34.                         "x-requested-with: XMLHttpRequest\r\n",
  35.             'content' => $postString
  36.         )
  37.     );
  38.     $context = stream_context_create($opts);
  39.     $result = file_get_contents('https://baza-otvetov.ru/quiz/check', false, $context);
  40.     $pravval = explode("<br /><br />", explode("<h3 style='color:#339966'>", $result)[1])[0];
  41.     if ($pravval == "Правильно!") $_val = $val;
  42.     array_push($vals, $val);
  43. }
  44. $resu = array(
  45.     'question'=> $question,
  46.     'answers'=> $vals,
  47.     'solve'=> $_val
  48. );
  49. echo json_encode($resu, JSON_UNESCAPED_UNICODE);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement