diff --git a/V2ex.php b/V2ex.php index e872d1d..6029219 100644 --- a/V2ex.php +++ b/V2ex.php @@ -11,38 +11,40 @@ class V2ex { private $u; private $p; private $cookie; - + private $isLogin; - + public static function init($u, $p, $cookie=self::COOKIE) { $v2ex = new V2ex(); $v2ex->u = $u; $v2ex->p = $p; - $v2ex->cookie = $cookie; + $v2ex->cookie = __DIR__ . $cookie; $v2ex->login(); return $v2ex; } - + public function login() { $html = $this->loginPage(); + preg_match('/用户名.+?name="(.+?)"/s', $html, $username_field); + preg_match('/密码.+?name="(.+?)"/s', $html, $password_field); $params = array( - 'u'=>$this->u, - 'p'=>$this->p, + $username_field[1]=>$this->u, + $password_field[1]=>$this->p, 'once'=>$this->getLoginCode($html), 'next'=>'/' ); $v2ex = $this->send(self::LOGIN_ACTION_URL, "POST", $params); $this->isLogin = true; } - + private function loginPage() { return $this->send(self::LOGIN_URL); } - + private function missionDailyPage() { return $this->send(self::MISSION_DAILY_URL); } - + public function missionDaily() { if($this->isLogin!==true) { $this->login(); @@ -56,7 +58,7 @@ public function missionDaily() { echo date('Y-m-d H:i:s')." mission url:".$url."\n"; $this->send($url); } - + private function getLoginCode($html) { $pattern = '/input\stype="hidden"\svalue="(\w{5})"\sname="once"/iu'; if(preg_match($pattern, $html, $matchs)) { @@ -64,7 +66,7 @@ private function getLoginCode($html) { } return !empty($code)?$code:false; } - + private function getMissionCode($html) { $pattern = '/mission\/daily\/redeem\?once=(\w{5})/iu'; if(preg_match($pattern, $html, $matchs)) { @@ -72,13 +74,15 @@ private function getMissionCode($html) { } return !empty($code)?$code:false; } - + private function send($url, $type='GET', $params=false) { $ch = curl_init($url); //初始化 curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); //发送cookies curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); //存储cookies + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); if($type==="POST") { curl_setopt($ch, CURLOPT_POST, 1); } diff --git a/data/v2ex.cookie b/data/v2ex.cookie new file mode 100644 index 0000000..e69de29