/** * 登录拿到cookie * @return mixed|null */ public function login() { is_dir(Helpers::C('home_path') . '/tmp') ?: mkdir(Helpers::C('home_path') . '/tmp'); $login_cookie_file = Helpers::C('home_path') . '/tmp/login_cookie.php'; // 如果有cookie文件 if (is_file($login_cookie_file)) { $cookie = (require $login_cookie_file); } else { // 登录 $data = 'user_acc=' . Helpers::C('login_account') . '&user_pwd=' . Helpers::C('login_password'); $subject = Helpers::httpPost(Helpers::C('login_page'), $data, Helpers::C('header')); $login_info_written = file_put_contents(Helpers::C('home_path') . '/tmp/login_info', $subject); if (!$login_info_written) { return null; } $pattern = "/Set-Cookie:\\s(\\w+?=[0-9a-zA-Z_%]+?);/is"; $has_cookie = preg_match($pattern, $subject, $arr); if (!$has_cookie) { return null; } $session_arr = "<?php\r\n return '{$arr['1']}';\r\n"; $cookie_written = file_put_contents($login_cookie_file, $session_arr); if (!$cookie_written) { return null; } $cookie = $arr[1]; } return $cookie; }
<?php require_once __DIR__ . '/vendor/autoload.php'; $config = \Utility\Helpers::merge(require __DIR__ . '/config/' . 'main.php', require __DIR__ . '/config/' . 'main-local.php'); \Utility\Helpers::C($config); $params = new \Utility\Assignment(); $params = $params->assign(); $controllerStr = '\\Controllers\\' . $params['controller']; $actionStr = $params['action']; // controller文件 $controllerFile = __DIR__ . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR . $params['controller'] . '.php'; is_file($controllerFile) ? $controllerObj = new $controllerStr() : die($controllerStr . '不存在'); method_exists($controllerObj, $actionStr) ? $controllerObj->{$actionStr}() : die($controllerStr . '->' . $actionStr . '不存在');