public function doLogin() { $curlObj = loadClass('zhCurl'); //获取登陆页 $loginSite = "https://www.zhihu.com/"; $loginHtml = $curlObj->getWebPage($loginSite); $html = loadClass('parserDom', $loginHtml['content']); $xsrf = $html->find('input[name=_xsrf]', 0)->getAttr('value'); unset($html); //获取验证码并从CLI输入 $captcha = "https://www.zhihu.com/captcha.gif?r=" . time() . rand(200, 999); $result = $curlObj->getWebPage($captcha); $captchaFile = dirname(__FILE__) . '/../res/login.gif'; $handle = fopen($captchaFile, 'w+'); fwrite($handle, $result['content']); fclose($handle); fwrite(STDOUT, "Pleate check the login.gif in project 'res' foler and enter it:\n"); $captchaContent = trim(fgets(STDIN)); $postParam = array('_xsrf' => $xsrf, 'email' => getConfig('zhAccount'), 'password' => getConfig('zhPassword'), 'remember_me' => 'true', 'captcha' => $captchaContent); $postUrl = 'https://www.zhihu.com/login/email'; $result = $curlObj->getWebPage($postUrl, array(CURLOPT_POSTFIELDS => buildParamFromArray($postParam))); $loginResult = json_decode($result['content'], TRUE); if ($loginResult['r'] == 0) { fwrite(STDOUT, "Login Success\n"); } else { fwrite(STDOUT, "Login Failed: {$loginResult['msg']}\n"); } return $xsrf; }
public function startGet() { $defaultParam = array('method' => 'next', 'params' => array('offset' => -20, 'order_by' => 'created', 'hash_id' => $this->hashId), '_xsrf' => XSRF); do { $defaultParam['params']['offset'] += 20; $postParam = $defaultParam; $postParam['params'] = urlencode(json_encode($postParam['params'])); $result = $this->getList(buildParamFromArray($postParam)); } while ($result); }
function buildParamFromArray($arrParam) { if (!is_array($arrParam)) { return FALSE; } $strResult = ''; foreach ($arrParam as $key => $val) { if (is_array($val)) { $val = buildParamFromArray($val); } $strResult .= $key . '=' . $val . '&'; } $strResult = substr($strResult, 0, strlen($strResult) - 1); return $strResult; }