/** * 获取收藏夹创建者 * @return object 创建者 */ public function author() { if (!empty($this->author)) { $author = $this->author; } else { $this->parser(); $author_link = $this->dom->find('h2.zm-list-content-title', 0); $author = parser_user($author_link); } return $author; }
/** * 获取点赞该回答的用户 * @return Generator 点赞用户迭代器 */ public function voters() { $answer_id = $this->aid(); $voters_url = ANSWERS_PREFIX_URL . $answer_id . VOTERS_SUFFIX_URL; while (true) { $r = Request::get($voters_url); $response_json = json_decode($r); if (!empty($response_json->paging->next)) { $voters_url = ZHIHU_URL . $response_json->paging->next; $voters = $response_json->payload; foreach ($voters as $voter) { $dom = str_get_html($voter); $voter_link = $dom->find('div.body', 0); (yield parser_user($voter_link)); } } else { break; } } }
/** * 获取问题关注者 * @return Generator 关注者列表迭代器 */ public function followers() { $followers_num = $this->followers_num(); if ($followers_num == 0) { (yield null); } else { $follwers_url = $this->url . FOLLOWERS_SUFFIX_URL; $r = Request::get($follwers_url); $dom = str_get_html($r); $_xsrf = _xsrf($dom); for ($i = 0; $i < $followers_num / 20; $i++) { if ($i == 0) { for ($j = 0; $j < min($followers_num, 20); $j++) { $follower_link = $dom->find('div.zm-profile-card h2 a', $j); (yield parser_user($follower_link)); } } else { $data = array('start' => 0, 'offset' => $i * 20, '_xsrf' => $_xsrf); $r = Request::post($follwers_url, $data, array("Referer: {$follwers_url}")); $r = json_decode($r)->msg; $dom = str_get_html($r[1]); for ($j = 0; $j < min($followers_num - $i * 20, 20); $j++) { $follower_link = $dom->find('div.zm-profile-card h2', $j); (yield parser_user($follower_link)); } } } } }
/** * 获取该话题下的最佳回答者 * @return array 最佳回答者列表 */ public function answerer() { $this->parser(); for ($i = 0; !empty($dom = $this->dom->find('div.zm-topic-side-person-item', $i)); $i++) { (yield parser_user($dom)); } }
/** * 获取用户列表 * @param String $type 关注或者粉丝 * @return Generator 用户列表 */ private function follow($type) { if ($type === 'FOLLOWERS') { $num = $this->followers_num(); $url = $this->url . FOLLOWERS_SUFFIX_URL; $post_url = FOLLOWERS_LIST_URL; } elseif ($type === 'FOLLOWEES') { $num = $this->followees_num(); $url = $this->url . FOLLOWEES_SUFFIX_URL; $post_url = FOLLOWEES_LIST_URL; } if ($num <= 0) { return null; } else { $r = Request::get($url); $dom = str_get_html($r); $_xsrf = _xsrf($dom); $json = $dom->find('div.zh-general-list', 0)->attr['data-init']; for ($i = 0; $i < $num / 20; $i++) { if ($i == 0) { for ($j = 0; $j < min($num, 20); $j++) { $user_list = $dom->find('h2.zm-list-content-title', $j); (yield parser_user($user_list)); } } else { $params = json_decode(html_entity_decode($json))->params; $params->offset = $i * 20; $params = json_encode($params); $data = array('_xsrf' => $_xsrf, 'method' => 'next', 'params' => $params); $r = Request::post($post_url, $data, array("Referer: {$url}")); $r = json_decode($r)->msg; for ($j = 0; $j < min($num - $i * 20, 20); $j++) { $dom = str_get_html($r[$j]); $user_list = $dom->find('h2.zm-list-content-title', 0); (yield parser_user($user_list)); } } } } }