function save_answer($base_url, $username, $answer_link_list) { foreach ($answer_link_list as $url) { $url = $base_url . $url; list($_, $content) = odie_get($url); list($question, $content) = parse_answer($content); echo "\t{$question}\n"; $content = '<link rel="stylesheet" href="http://static.zhihu.com/static/ver/f004e446ca569e4897e59cf26da3e2dc.z.css" type="text/css" media="screen,print" />' . $content . '<div><a href="' . $url . '">原链接</a></div>' . '<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>' . '<script>$("img").each(function(){$(this).attr("src",$(this).attr("data-actualsrc"))});</script>'; $root = __DIR__ . "/{$username}"; if (!is_dir($root)) { mkdir($root); } $question = mb_strimwidth($question, 0, 40, '...', 'utf8'); $question = str_replace('/', '-', $question); file_put_contents("{$root}/{$question}.html", $content); } }
if (!is_empty($row["ProblemSelectE"])) { array_push($selectors, array("id" => "E", "content" => $row["ProblemSelectE"])); } if (!is_empty($row["ProblemSelectF"])) { array_push($selectors, array("id" => "F", "content" => $row["ProblemSelectF"])); } if (!is_empty($row["ProblemSelectG"])) { array_push($selectors, array("id" => "G", "content" => $row["ProblemSelectG"])); } if (!is_empty($row["ProblemSelectH"])) { array_push($selectors, array("id" => "H", "content" => $row["ProblemSelectH"])); } if (!is_empty($row["ProblemSelectI"])) { array_push($selectors, array("id" => "I", "content" => $row["ProblemSelectI"])); } $answers = parse_answer($row["ProblemAnswer"]); //$score = $type_scores[$row["ProblemLevel"]]; $score = 2; array_push($problems, array("id" => (int) $row["ProblemId"], "description" => $row["ProblemDesc"], "level" => (int) $row["ProblemLevel"], "type" => (int) $row["ProblemType"], "score" => (int) $score, "selectors" => $selectors, "answer" => $answers, "memo" => $row["ProblemMemo"])); /* // insert into ExamDetail, insert score if (insert_into_examdetail($exam_id, $prob_id, $score) != SUCCESS) { if($link){ mysqli_close($link); } sleep(DELAY_SEC); //echo -__LINE__; echo ERR_INSERT_DATABASE; return; }*/
/** * Sends an HTTP POST request * * @param string $host Host address * @param string $port Port number * @param string $path Request path * @param string $header Request header * @param string $body Request body * @return Answer of remote host */ function send_http_post($host, $port, $path, $header, $body) { # Open the connection using Internet socket connection $fp = @fsockopen($host, $port, $errno, $errstr); if (!$fp) { fatal("<p>Unable to establish connection to <tt>{$host}</tt> " . "(port <tt>{$port}</tt>):</p><pre>{$errstr}</pre>"); } # Send HTTP message headers fwrite($fp, "POST {$path} HTTP/1.0\r\n"); fwrite($fp, $header); fwrite($fp, "\r\n"); # Send HTTP message body fwrite($fp, $body); # Read response from the server $buf = ''; $length = false; while (!feof($fp)) { $buf .= fread($fp, 8192); # Parse the HTTP answer $answer = parse_answer($buf); # Find out about content length if (!$length) { if ($answer[0] == 200) { $length = $answer[1]['content-length']; } } # Was response fully transmitted or not if ($length) { if (strlen($buf) == strlen($answer[3]) + $length) { break; } } } fclose($fp); return $answer; }