├── .gitignore ├── ChangeLog.md ├── LICENSE ├── README.md ├── test ├── test_answer.php ├── test_collection.php ├── test_question.php ├── test_topic.php ├── test_user.php └── test_zhihu.php └── zhihu ├── answer.php ├── collection.php ├── libraries ├── request.php └── simple_html_dom.php ├── question.php ├── topic.php ├── user.php └── zhihu.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /docs -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | ## Change Log 2 | 3 | ### 2016.04.12 4 | - Remove 由于知乎专栏发布正式版,移除 Colunm 以及 Post 类 5 | - update 更新 test_zhihu,删除移除的专栏相关测试 6 | - ToDo Question 中存在 Bug,待修复 7 | 8 | ### 2016.02.28 9 | - Update 更新其他类测试,增加测试数据输出 10 | - Remove 移除 Comment 类,待以后添加 11 | - Fix 修复 Collection 中获取收藏夹作者时的错误 12 | 13 | ### 2016.02.23 14 | - Update 更新 test_answer、test_question, 增加测试数据输出 15 | - ~~ToDO 修复 Comment~~ 16 | 17 | ### 2016.02.14 18 | - Update 更新 test_user, 增加测试数据输出 19 | 20 | ### 2016.02.11 21 | - Finish 完成 Post 类及测试 22 | 23 | ### 2016.02.08 24 | - Add 增加知乎专栏类 Column 与知乎文章类 Post 25 | - Add 增加专栏相关测试 26 | 27 | ### 2016.02.02 28 | - Change 修改 test_topic.php 29 | - Update 精简 Topic 类冗余代码 30 | 31 | ### 2016.02.01 32 | - Change 修改 test_question.php,并修复 Question 中的小 bug 33 | - Fix 由于知乎问题回答下默认显示条数由 50 变为 20,修复 Question->answers() 中的 bug 34 | 35 | ### 2016.01.31 36 | - Change 修改 test_answer.php,使用 phpunit 进行测试 37 | - Change 将 Answer 中获取 aid 单独为一个 aid() 函数,并添加 private aid 属性 38 | - Add 更新 Answer 类,添加 comments_num() 函数获取评论数 39 | - Fix 修复回答下的评论回复与被回复者匿名时产生的 Bug 40 | 41 | ### 2016.01.27 42 | - Add User 类添加获取用户收藏夹函数 collections() 43 | 44 | ### 2016.01.22 45 | - Update 更新 Topic 类,添加 parser_entire() 等 46 | 47 | ### 2016.01.21 48 | - Update 更新 answer 类,替换重复代码 49 | - Fix 修改所有类的 url 属性为 private,并添加 url() 函数 50 | - Fix 添加 dom 属性 51 | 52 | ### 2016.01.20 53 | - Fix 重写 question 类获取关注者及回答相关函数 54 | - Add 增加获取问题关注者及回答的公共函数 55 | 56 | ### 2016.01.15 57 | - Update 去除所有类中函数名的 `get` 前缀 58 | - Add User 类增加获取用户微博链接 59 | - Fix 重写 User 类获取用户信息相关函数 60 | - Fix 修复 Collection 类中因为回答建议修改而产生的错误,其他类暂时还没有修复 61 | 62 | ### 2016.01.13 63 | - Fix 根据知乎前端回答评论更新为分页显示,修复获取回答评论时的 Bug 64 | 65 | ### 2016.01.11 66 | - Add 添加 Comment 类测试 67 | - CHange 修改 Comment 类中的 reply 为 replyed 68 | 69 | ### 2016.01.09 70 | - Fix 完善 Collection 类 get_answers() 71 | 72 | ### 2016.01.08 73 | - Add 添加获取话题最新问题 get_new_question() 74 | - Fix 修复 Topic 中获取精华回答的错误,修改 get_top_question() 为 get_top_answer() 75 | 76 | ### 2016.01.07 77 | - Update 更新 Topic 中的 get_hot_question(),get_top_question()以及 get_all_question() 使它们能够获取所有问题 78 | 79 | ### 2016.01.06 80 | - Add 在 Answer 类中添加获取点赞用户的函数 get_voters() 81 | 82 | ### 2016.01.05 83 | - Update 去除所有类中的属性名称前缀 84 | - Add 添加遗漏的 Topic 类中的 get_name() 函数 85 | 86 | ### 2016.01.03 87 | - Fix 修复每次获取数据时都重新解析网页的 Bug 88 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zhihu-php 2 | 3 | ## 介绍 4 | zhihu-php 使用PHP编写,用于获取知乎上的各种信息。 5 | 6 | **本项目需要 `PHP version >= 5.3` ,开启 `CURL` 扩展,使用 `simple html dom` 解析网页** 7 | 8 | 根据 URL 生成对应的对象,例如知乎用户对象 User、知乎问题对象 Question、答案对象 Answer 等等。 9 | 10 | ## 准备 11 | 打开 `zhihu.php`,将知乎的 Cookie 复制黏贴到 `COOKIE` 的常量定义中: 12 | ``` php 13 | const COOKIE = "your cookie"; 14 | ``` 15 | 16 | ## 简单样例 17 | ``` php 18 | top_answer(); 29 | 30 | // 获取该回答的答主 31 | $author = $answer->author(); 32 | 33 | // 获取答案答主的信息 34 | $about = $author->about(); 35 | 36 | // 输出答主基本信息 37 | var_dump($about); 38 | ``` 39 | 40 | ## 更新信息 41 | [Change Log](https://github.com/ahonn/zhihu-php/blob/master/ChangeLog.md) 42 | 43 | ## 联系我 44 | - Email:[ahonn95@outlook.com](mailto:ahonn95@outlook.com) 45 | 46 | -------------------------------------------------------------------------------- /test/test_answer.php: -------------------------------------------------------------------------------- 1 | url = 'https://www.zhihu.com/question/24825703/answer/30975949'; 13 | $this->answer = new Answer($this->url); 14 | } 15 | 16 | public function testQuestion() 17 | { 18 | $question = $this->answer->question(); 19 | printf("%s \n", $question->title()); 20 | 21 | $this->assertInstanceOf('Question', $question); 22 | } 23 | 24 | public function testAuthor() 25 | { 26 | $author = $this->answer->author(); 27 | printf("%s \n", $author->name()); 28 | 29 | $this->assertInstanceOf('User', $author); 30 | } 31 | 32 | public function testContent() 33 | { 34 | $content = $this->answer->content(); 35 | printf("%s \n", $content); 36 | 37 | $this->assertNotEmpty($content); 38 | } 39 | 40 | public function testUpvote() 41 | { 42 | $upvote = $this->answer->upvote(); 43 | $this->assertInternalType('int', $upvote); 44 | printf("%d \n", $upvote); 45 | 46 | $voters = $this->answer->voters(); 47 | $count = 0; 48 | foreach ($voters as $voter) { 49 | $count++; 50 | $this->assertInstanceOf('User', $voter); 51 | printf("%s \n", $voter->name()); 52 | } 53 | $this->assertLessThanOrEqual($upvote, $count); 54 | } 55 | 56 | public function testCollection() 57 | { 58 | $collections_num = $this->answer->collections_num(); 59 | $this->assertInternalType('int', $collections_num); 60 | printf("%d \n", $collections_num); 61 | 62 | $collections = $this->answer->collections(); 63 | $count = 0; 64 | foreach ($collections as $collection) { 65 | $count++; 66 | $this->assertInstanceOf('Collection', $collection); 67 | printf("%s \n", $collection->title()); 68 | } 69 | $this->assertLessThanOrEqual($collections_num, $count); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /test/test_collection.php: -------------------------------------------------------------------------------- 1 | url = 'https://www.zhihu.com/collection/19650606'; 11 | $this->collection = new Collection($this->url); 12 | } 13 | 14 | public function testTitle() 15 | { 16 | $title = $this->collection->title(); 17 | $title_tmp = '妙趣横生'; 18 | printf("%s \n", $title); 19 | 20 | $this->assertSame($title_tmp, $title); 21 | } 22 | 23 | public function testDesc() 24 | { 25 | $desc = $this->collection->desc(); 26 | $desc_tmp = '嬉笑怒骂,剑走偏锋,思路切入显大巧,文字构筑含义深。'; 27 | printf("%s \n", $desc); 28 | 29 | $this->assertSame($desc_tmp, $desc); 30 | } 31 | 32 | public function testAuthor() 33 | { 34 | $author = $this->collection->author(); 35 | $this->assertInstanceOf('User', $author); 36 | 37 | $author_name = '杨凯光'; 38 | printf("%s \n", $author->name()); 39 | $this->assertSame($author_name, $author->name()); 40 | } 41 | 42 | 43 | public function testAnswer() 44 | { 45 | $answers = $this->collection->answers(); 46 | for($i = 0; $i < 200; $i++) { 47 | $answer = $answers->current(); 48 | $answers->next(); 49 | $this->assertInstanceOf('Answer', $answer); 50 | printf("%s \n", $answer->question()->title()); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /test/test_question.php: -------------------------------------------------------------------------------- 1 | url = 'https://www.zhihu.com/question/27187478'; 14 | $this->question = new Question($this->url); 15 | } 16 | 17 | public function testTitle() 18 | { 19 | $title = $this->question->title(); 20 | $title_tmp = '徒手码一千行以上代码是一种怎样的体验?'; 21 | printf("%s \n", $title); 22 | 23 | $this->assertSame($title_tmp, $title); 24 | } 25 | 26 | public function testDetail() 27 | { 28 | $detail = $this->question->detail(); 29 | $detail_tmp = '在学C语言(非本专业),感觉看着代码一行一行码下来好爽啊。不知道徒手码一千行以上的代码是个什么感觉呢?有人试过吗?'; 30 | printf("%s \n", $detail); 31 | 32 | $this->assertSame($detail_tmp, $detail); 33 | } 34 | 35 | public function testAnswer() 36 | { 37 | $answers_num = $this->question->answers_num(); 38 | $this->assertInternalType('int', $answers_num); 39 | printf("%d \n", $answers_num); 40 | 41 | $answers = $this->question->answers(); 42 | $count = 0; 43 | foreach ($answers as $answer) { 44 | $count++; 45 | $this->assertInstanceOf('Answer', $answer); 46 | printf("%s \n", $answer->author()->name()); 47 | } 48 | $this->assertGreaterThanOrEqual($answers_num, $count); 49 | } 50 | 51 | public function testFollower() 52 | { 53 | $followers_num = $this->question->followers_num(); 54 | $this->assertInternalType('int', $followers_num); 55 | printf("%d \n", $followers_num); 56 | 57 | $followers = $this->question->followers(); 58 | $count = 0; 59 | foreach ($followers as $follower) { 60 | $count++; 61 | $this->assertInstanceOf('User', $follower); 62 | printf("%s \n", $follower->name()); 63 | } 64 | $this->assertLessThanOrEqual($followers_num, $count); 65 | } 66 | 67 | public function testTopic() 68 | { 69 | $topics = $this->question->topics(); 70 | $topics_tmp = array("编程", "编程学习"); 71 | $this->assertSame($topics_tmp, $topics); 72 | } 73 | } -------------------------------------------------------------------------------- /test/test_topic.php: -------------------------------------------------------------------------------- 1 | url = 'https://www.zhihu.com/topic/19552330'; 11 | $this->topic = new Topic($this->url); 12 | } 13 | 14 | public function testName() 15 | { 16 | $name = $this->topic->name(); 17 | printf("%s \n", $name); 18 | $name_tmp = '程序员'; 19 | 20 | $this->assertSame($name_tmp, $name); 21 | } 22 | 23 | public function testDesc() 24 | { 25 | $desc = $this->topic->desc(); 26 | printf("%s \n", $desc); 27 | $desc_tmp = '程序员可以指在程序设计某个专业领域中的专业人士或是从事软件撰写,程序开发、维护的专业人员。'; 28 | 29 | $this->assertSame($desc_tmp, $desc); 30 | } 31 | 32 | public function testFollower() 33 | { 34 | $followers_num = $this->topic->followers_num(); 35 | printf("%d \n", $followers_num); 36 | $this->assertInternalType('int', $followers_num); 37 | } 38 | 39 | public function testParentAndChildren() 40 | { 41 | $parents = $this->topic->parents(); 42 | foreach ($parents as $parent) { 43 | printf("%s \n", $parent->name()); 44 | $this->assertInstanceOf('Topic', $parent); 45 | } 46 | 47 | $childrens = $this->topic->childrens(); 48 | foreach ($childrens as $children) { 49 | printf("%s \n", $children->name()); 50 | $this->assertInstanceOf('Topic', $children); 51 | } 52 | } 53 | 54 | public function testAnswerer() 55 | { 56 | $answerer = $this->topic->answerer(); 57 | foreach ($answerer as $user) { 58 | printf("%s \n", $user->name()); 59 | $this->assertInstanceOf('User', $user); 60 | } 61 | } 62 | 63 | public function testQuestion() 64 | { 65 | $hot_question = $this->topic->hot_question(); 66 | for($i = 0; $i < 200; $i++) { 67 | $question = $hot_question->current(); 68 | printf("%s \n", $question->title()); 69 | $hot_question->next(); 70 | $this->assertInstanceOf('Question', $question); 71 | } 72 | 73 | $new_question = $this->topic->new_question(); 74 | for($i = 0; $i < 200; $i++) { 75 | $question = $new_question->current(); 76 | printf("%s \n", $question->title()); 77 | $new_question->next(); 78 | $this->assertInstanceOf('Question', $question); 79 | } 80 | 81 | $all_question = $this->topic->all_question(); 82 | for($i = 0; $i < 200; $i++) { 83 | $question = $all_question->current(); 84 | printf("%s \n", $question->title()); 85 | $all_question->next(); 86 | $this->assertInstanceOf('Question', $question); 87 | } 88 | } 89 | 90 | public function testAnswer() 91 | { 92 | $top_answer = $this->topic->top_answer(); 93 | for($i = 0; $i < 200; $i++) { 94 | $answer = $top_answer->current(); 95 | printf("%s \n", $answer->question()->title()); 96 | $top_answer->next(); 97 | $this->assertInstanceOf('Answer', $answer); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /test/test_user.php: -------------------------------------------------------------------------------- 1 | url = 'https://www.zhihu.com/people/ahonn'; 13 | $this->user = new User($this->url); 14 | } 15 | 16 | public function testAbout() 17 | { 18 | $about = $this->user->about(); 19 | 20 | $this->assertArrayHasKey('name', $about); 21 | $this->assertArrayHasKey('avatar', $about); 22 | $this->assertArrayHasKey('weibo_url', $about); 23 | $this->assertArrayHasKey('location', $about); 24 | $this->assertArrayHasKey('business', $about); 25 | $this->assertArrayHasKey('gender', $about); 26 | $this->assertArrayHasKey('employment', $about); 27 | $this->assertArrayHasKey('position', $about); 28 | $this->assertArrayHasKey('education', $about); 29 | $this->assertArrayHasKey('major', $about); 30 | $this->assertArrayHasKey('desc', $about); 31 | 32 | foreach ($about as $info) { 33 | printf("%s \n", $info); 34 | } 35 | } 36 | 37 | public function testFollowee() 38 | { 39 | $followees_num = $this->user->followees_num(); 40 | $this->assertInternalType('int', $followees_num); 41 | printf("%d \n", $followees_num); 42 | 43 | $followees = $this->user->followees(); 44 | $count = 0; 45 | foreach ($followees as $followee) { 46 | $count++; 47 | $this->assertInstanceOf('User', $followee); 48 | printf("%s \n", $followee->name()); 49 | } 50 | $this->assertGreaterThanOrEqual($followees_num, $count); 51 | } 52 | 53 | public function testFollower() 54 | { 55 | $followers_num = $this->user->followers_num(); 56 | $this->assertInternalType('int', $followers_num); 57 | printf("%d \n", $followers_num); 58 | 59 | $followers = $this->user->followers(); 60 | $count = 0; 61 | foreach ($followers as $follower) { 62 | $count++; 63 | $this->assertInstanceOf('User', $follower); 64 | printf("%s \n", $follower->name()); 65 | } 66 | $this->assertGreaterThanOrEqual($followers_num, $count); 67 | } 68 | 69 | public function testAgree() 70 | { 71 | $agree_num = $this->user->agree_num(); 72 | $this->assertInternalType('int', $agree_num); 73 | printf("%d \n", $agree_num); 74 | } 75 | 76 | public function testThanks() 77 | { 78 | $thanks_num = $this->user->thanks_num(); 79 | $this->assertInternalType('int', $thanks_num); 80 | printf("%d \n", $thanks_num); 81 | } 82 | 83 | public function testAsks() 84 | { 85 | $asks_num = $this->user->asks_num(); 86 | $this->assertInternalType('int', $asks_num); 87 | printf("%d \n", $asks_num); 88 | 89 | $asks = $this->user->asks(); 90 | $count = 0; 91 | foreach ($asks as $ask) { 92 | $count++; 93 | $this->assertInstanceOf('Question', $ask); 94 | printf("%s \n", $ask->title()); 95 | } 96 | $this->assertGreaterThanOrEqual($asks_num, $count); 97 | } 98 | 99 | public function testAnswers() 100 | { 101 | $answers_num = $this->user->answers_num(); 102 | $this->assertInternalType('int', $answers_num); 103 | printf("%d \n", $answers_num); 104 | 105 | $answers = $this->user->answers(); 106 | $count = 0; 107 | foreach ($answers as $answer) { 108 | $count++; 109 | $this->assertInstanceOf('Answer', $answer); 110 | printf("%s \n", $answer->question()->title()); 111 | } 112 | $this->assertGreaterThanOrEqual($answers_num, $count); 113 | } 114 | 115 | public function testColumn() 116 | { 117 | $columns = $this->user->columns(); 118 | foreach ($columns as $column) { 119 | $this->assertInstanceOf('Column', $column); 120 | printf("%s \n", $column->name()); 121 | } 122 | } 123 | 124 | public function testPosts() 125 | { 126 | $posts_num = $this->user->posts_num(); 127 | $this->assertInternalType('int', $posts_num); 128 | printf("%d \n", $posts_num); 129 | 130 | if($posts_num != 0) { 131 | $posts = $this->user->posts(); 132 | $count = 0; 133 | foreach ($posts as $post) { 134 | $count++; 135 | $this->assertInstanceOf('Post', $post); 136 | printf("%s \n", $post->title()); 137 | } 138 | $this->assertGreaterThanOrEqual($posts_num, $count); 139 | } 140 | } 141 | 142 | public function testCollection() 143 | { 144 | $collections_num = $this->user->collections_num(); 145 | $this->assertInternalType('int', $collections_num); 146 | printf("%d \n", $collections_num); 147 | 148 | $collections = $this->user->collections(); 149 | $count = 0; 150 | foreach ($collections as $collection) { 151 | $count++; 152 | $this->assertInstanceOf('Collection', $collection); 153 | printf("%s \n", $collection->title()); 154 | } 155 | $this->assertGreaterThanOrEqual($collections_num, $count); 156 | } 157 | 158 | public function testTopic() 159 | { 160 | $topics_num = $this->user->topics_num(); 161 | $this->assertNotEmpty($topics_num); 162 | printf("%d \n", $topics_num); 163 | 164 | $topics = $this->user->topics(); 165 | $count = 0; 166 | foreach ($topics as $topic) { 167 | $count++; 168 | $this->assertInstanceOf('Topic', $topic); 169 | printf("%s \n", $topic->name()); 170 | } 171 | $this->assertGreaterThanOrEqual($topics_num, $count); 172 | } 173 | } -------------------------------------------------------------------------------- /test/test_zhihu.php: -------------------------------------------------------------------------------- 1 | user = new User('https://www.zhihu.com/people/ahonn'); 18 | $this->question = new Question('https://www.zhihu.com/question/27187478'); 19 | $this->answer = new Answer('https://www.zhihu.com/question/24825703/answer/30975949'); 20 | $this->topic = new Topic('https://www.zhihu.com/topic/19552330'); 21 | $this->collection = new Collection('https://www.zhihu.com/collection/19650606'); 22 | } 23 | 24 | ///////////////// User /////////////////// 25 | 26 | public function testUserAbout() 27 | { 28 | $about = $this->user->about(); 29 | 30 | $this->assertArrayHasKey('name', $about); 31 | $this->assertArrayHasKey('avatar', $about); 32 | $this->assertArrayHasKey('weibo_url', $about); 33 | $this->assertArrayHasKey('location', $about); 34 | $this->assertArrayHasKey('business', $about); 35 | $this->assertArrayHasKey('gender', $about); 36 | $this->assertArrayHasKey('employment', $about); 37 | $this->assertArrayHasKey('position', $about); 38 | $this->assertArrayHasKey('education', $about); 39 | $this->assertArrayHasKey('major', $about); 40 | $this->assertArrayHasKey('desc', $about); 41 | 42 | foreach ($about as $info) { 43 | printf("%s \n", $info); 44 | } 45 | } 46 | 47 | public function testUserFollowee() 48 | { 49 | $followees_num = $this->user->followees_num(); 50 | $this->assertInternalType('int', $followees_num); 51 | printf("%d \n", $followees_num); 52 | 53 | $followees = $this->user->followees(); 54 | $count = 0; 55 | foreach ($followees as $followee) { 56 | $count++; 57 | $this->assertInstanceOf('User', $followee); 58 | printf("%s \n", $followee->name()); 59 | } 60 | $this->assertGreaterThanOrEqual($followees_num, $count); 61 | } 62 | 63 | public function testUserFollower() 64 | { 65 | $followers_num = $this->user->followers_num(); 66 | $this->assertInternalType('int', $followers_num); 67 | printf("%d \n", $followers_num); 68 | 69 | $followers = $this->user->followers(); 70 | $count = 0; 71 | foreach ($followers as $follower) { 72 | $count++; 73 | $this->assertInstanceOf('User', $follower); 74 | printf("%s \n", $follower->name()); 75 | } 76 | $this->assertGreaterThanOrEqual($followers_num, $count); 77 | } 78 | 79 | public function testUserAgree() 80 | { 81 | $agree_num = $this->user->agree_num(); 82 | $this->assertInternalType('int', $agree_num); 83 | printf("%d \n", $agree_num); 84 | } 85 | 86 | public function testUserThanks() 87 | { 88 | $thanks_num = $this->user->thanks_num(); 89 | $this->assertInternalType('int', $thanks_num); 90 | printf("%d \n", $thanks_num); 91 | } 92 | 93 | public function testUserAsks() 94 | { 95 | $asks_num = $this->user->asks_num(); 96 | $this->assertInternalType('int', $asks_num); 97 | printf("%d \n", $asks_num); 98 | 99 | $asks = $this->user->asks(); 100 | $count = 0; 101 | foreach ($asks as $ask) { 102 | $count++; 103 | $this->assertInstanceOf('Question', $ask); 104 | printf("%s \n", $ask->title()); 105 | } 106 | $this->assertGreaterThanOrEqual($asks_num, $count); 107 | } 108 | 109 | public function testUserAnswers() 110 | { 111 | $answers_num = $this->user->answers_num(); 112 | $this->assertInternalType('int', $answers_num); 113 | printf("%d \n", $answers_num); 114 | 115 | $answers = $this->user->answers(); 116 | $count = 0; 117 | foreach ($answers as $answer) { 118 | $count++; 119 | $this->assertInstanceOf('Answer', $answer); 120 | printf("%s \n", $answer->question()->title()); 121 | } 122 | $this->assertGreaterThanOrEqual($answers_num, $count); 123 | } 124 | 125 | public function testUserColumn() 126 | { 127 | $columns = $this->user->columns(); 128 | foreach ($columns as $column) { 129 | $this->assertInstanceOf('Column', $column); 130 | printf("%s \n", $column->name()); 131 | } 132 | } 133 | 134 | public function testUserPosts() 135 | { 136 | $posts_num = $this->user->posts_num(); 137 | $this->assertInternalType('int', $posts_num); 138 | printf("%d \n", $posts_num); 139 | 140 | if($posts_num != 0) { 141 | $posts = $this->user->posts(); 142 | $count = 0; 143 | foreach ($posts as $post) { 144 | $count++; 145 | $this->assertInstanceOf('Post', $post); 146 | printf("%s \n", $post->title()); 147 | } 148 | $this->assertGreaterThanOrEqual($posts_num, $count); 149 | } 150 | } 151 | 152 | public function testUserCollection() 153 | { 154 | $collections_num = $this->user->collections_num(); 155 | $this->assertInternalType('int', $collections_num); 156 | printf("%d \n", $collections_num); 157 | 158 | $collections = $this->user->collections(); 159 | $count = 0; 160 | foreach ($collections as $collection) { 161 | $count++; 162 | $this->assertInstanceOf('Collection', $collection); 163 | printf("%s \n", $collection->title()); 164 | } 165 | $this->assertGreaterThanOrEqual($collections_num, $count); 166 | } 167 | 168 | public function testUserTopic() 169 | { 170 | $topics_num = $this->user->topics_num(); 171 | $this->assertNotEmpty($topics_num); 172 | printf("%d \n", $topics_num); 173 | 174 | $topics = $this->user->topics(); 175 | $count = 0; 176 | foreach ($topics as $topic) { 177 | $count++; 178 | $this->assertInstanceOf('Topic', $topic); 179 | printf("%s \n", $topic->name()); 180 | } 181 | $this->assertGreaterThanOrEqual($topics_num, $count); 182 | } 183 | 184 | ///////////////// Question /////////////////// 185 | 186 | public function testQuestionTitle() 187 | { 188 | $title = $this->question->title(); 189 | $title_tmp = '徒手码一千行以上代码是一种怎样的体验?'; 190 | printf("%s \n", $title); 191 | 192 | $this->assertSame($title_tmp, $title); 193 | } 194 | 195 | public function testQuestionDetail() 196 | { 197 | $detail = $this->question->detail(); 198 | $detail_tmp = '在学C语言(非本专业),感觉看着代码一行一行码下来好爽啊。不知道徒手码一千行以上的代码是个什么感觉呢?有人试过吗?'; 199 | printf("%s \n", $detail); 200 | 201 | $this->assertSame($detail_tmp, $detail); 202 | } 203 | 204 | public function testQuestionAnswer() 205 | { 206 | $answers_num = $this->question->answers_num(); 207 | $this->assertInternalType('int', $answers_num); 208 | printf("%d \n", $answers_num); 209 | 210 | $answers = $this->question->answers(); 211 | $count = 0; 212 | foreach ($answers as $answer) { 213 | $count++; 214 | $this->assertInstanceOf('Answer', $answer); 215 | printf("%s \n", $answer->author()->name()); 216 | } 217 | $this->assertGreaterThanOrEqual($answers_num, $count); 218 | } 219 | 220 | public function testQuestionFollower() 221 | { 222 | $followers_num = $this->question->followers_num(); 223 | $this->assertInternalType('int', $followers_num); 224 | printf("%d \n", $followers_num); 225 | 226 | $followers = $this->question->followers(); 227 | $count = 0; 228 | foreach ($followers as $follower) { 229 | $count++; 230 | $this->assertInstanceOf('User', $follower); 231 | printf("%s \n", $follower->name()); 232 | } 233 | $this->assertLessThanOrEqual($followers_num, $count); 234 | } 235 | 236 | public function testQuestionTopic() 237 | { 238 | $topics = $this->question->topics(); 239 | $topics_tmp = array("编程", "编程学习"); 240 | $this->assertSame($topics_tmp, $topics); 241 | } 242 | 243 | ///////////////// Answer /////////////////// 244 | 245 | public function testAnswerQuestion() 246 | { 247 | $question = $this->answer->question(); 248 | printf("%s \n", $question->title()); 249 | 250 | $this->assertInstanceOf('Question', $question); 251 | } 252 | 253 | public function testAnswerAuthor() 254 | { 255 | $author = $this->answer->author(); 256 | printf("%s \n", $author->name()); 257 | 258 | $this->assertInstanceOf('User', $author); 259 | } 260 | 261 | public function testAnswerContent() 262 | { 263 | $content = $this->answer->content(); 264 | printf("%s \n", $content); 265 | 266 | $this->assertNotEmpty($content); 267 | } 268 | 269 | public function testAnswerUpvote() 270 | { 271 | $upvote = $this->answer->upvote(); 272 | $this->assertInternalType('int', $upvote); 273 | printf("%d \n", $upvote); 274 | 275 | $voters = $this->answer->voters(); 276 | $count = 0; 277 | foreach ($voters as $voter) { 278 | $count++; 279 | $this->assertInstanceOf('User', $voter); 280 | printf("%s \n", $voter->name()); 281 | } 282 | $this->assertLessThanOrEqual($upvote, $count); 283 | } 284 | 285 | public function testAnswerCollection() 286 | { 287 | $collections_num = $this->answer->collections_num(); 288 | $this->assertInternalType('int', $collections_num); 289 | printf("%d \n", $collections_num); 290 | 291 | $collections = $this->answer->collections(); 292 | $count = 0; 293 | foreach ($collections as $collection) { 294 | $count++; 295 | $this->assertInstanceOf('Collection', $collection); 296 | printf("%s \n", $collection->title()); 297 | } 298 | $this->assertLessThanOrEqual($collections_num, $count); 299 | } 300 | 301 | ///////////////// Topic /////////////////// 302 | 303 | public function testTopicName() 304 | { 305 | $name = $this->topic->name(); 306 | printf("%s \n", $name); 307 | $name_tmp = '程序员'; 308 | 309 | $this->assertSame($name_tmp, $name); 310 | } 311 | 312 | public function testTopicDesc() 313 | { 314 | $desc = $this->topic->desc(); 315 | printf("%s \n", $desc); 316 | $desc_tmp = '程序员可以指在程序设计某个专业领域中的专业人士或是从事软件撰写,程序开发、维护的专业人员。'; 317 | 318 | $this->assertSame($desc_tmp, $desc); 319 | } 320 | 321 | public function testTopicFollower() 322 | { 323 | $followers_num = $this->topic->followers_num(); 324 | printf("%d \n", $followers_num); 325 | $this->assertInternalType('int', $followers_num); 326 | } 327 | 328 | public function testTopicParentAndChildren() 329 | { 330 | $parents = $this->topic->parents(); 331 | foreach ($parents as $parent) { 332 | printf("%s \n", $parent->name()); 333 | $this->assertInstanceOf('Topic', $parent); 334 | } 335 | 336 | $childrens = $this->topic->childrens(); 337 | foreach ($childrens as $children) { 338 | printf("%s \n", $children->name()); 339 | $this->assertInstanceOf('Topic', $children); 340 | } 341 | } 342 | 343 | public function testTopicAnswerer() 344 | { 345 | $answerer = $this->topic->answerer(); 346 | foreach ($answerer as $user) { 347 | printf("%s \n", $user->name()); 348 | $this->assertInstanceOf('User', $user); 349 | } 350 | } 351 | 352 | public function testTopicQuestion() 353 | { 354 | $hot_question = $this->topic->hot_question(); 355 | for($i = 0; $i < 200; $i++) { 356 | $question = $hot_question->current(); 357 | printf("%s \n", $question->title()); 358 | $hot_question->next(); 359 | $this->assertInstanceOf('Question', $question); 360 | } 361 | 362 | $new_question = $this->topic->new_question(); 363 | for($i = 0; $i < 200; $i++) { 364 | $question = $new_question->current(); 365 | printf("%s \n", $question->title()); 366 | $new_question->next(); 367 | $this->assertInstanceOf('Question', $question); 368 | } 369 | 370 | $all_question = $this->topic->all_question(); 371 | for($i = 0; $i < 200; $i++) { 372 | $question = $all_question->current(); 373 | printf("%s \n", $question->title()); 374 | $all_question->next(); 375 | $this->assertInstanceOf('Question', $question); 376 | } 377 | } 378 | 379 | public function testTopicAnswer() 380 | { 381 | $top_answer = $this->topic->top_answer(); 382 | for($i = 0; $i < 200; $i++) { 383 | $answer = $top_answer->current(); 384 | printf("%s \n", $answer->question()->title()); 385 | $top_answer->next(); 386 | $this->assertInstanceOf('Answer', $answer); 387 | } 388 | } 389 | 390 | ////////////// Collection ///////////////// 391 | 392 | public function testCollectionTitle() 393 | { 394 | $title = $this->collection->title(); 395 | $title_tmp = '妙趣横生'; 396 | printf("%s \n", $title); 397 | 398 | $this->assertSame($title_tmp, $title); 399 | } 400 | 401 | public function testCollectionDesc() 402 | { 403 | $desc = $this->collection->desc(); 404 | $desc_tmp = '嬉笑怒骂,剑走偏锋,思路切入显大巧,文字构筑含义深。'; 405 | printf("%s \n", $desc); 406 | 407 | $this->assertSame($desc_tmp, $desc); 408 | } 409 | 410 | public function testCollectionAuthor() 411 | { 412 | $author = $this->collection->author(); 413 | $this->assertInstanceOf('User', $author); 414 | 415 | $author_name = '杨凯光'; 416 | printf("%s \n", $author->name()); 417 | $this->assertSame($author_name, $author->name()); 418 | } 419 | 420 | 421 | public function testCollectionAnswer() 422 | { 423 | $answers = $this->collection->answers(); 424 | for($i = 0; $i < 200; $i++) { 425 | $answer = $answers->current(); 426 | $answers->next(); 427 | $this->assertInstanceOf('Answer', $answer); 428 | printf("%s \n", $answer->question()->title()); 429 | } 430 | } 431 | } 432 | -------------------------------------------------------------------------------- /zhihu/answer.php: -------------------------------------------------------------------------------- 1 | url = $url; 19 | if ( ! empty($question)) { 20 | $this->question = $question; 21 | } 22 | if ( ! empty($author)) { 23 | $this->author = $author; 24 | } 25 | if ( ! empty($upvote)) { 26 | $this->upvote = $upvote; 27 | } 28 | if ( ! empty($content)) { 29 | $this->content = $content; 30 | } 31 | } 32 | 33 | /** 34 | * 解析user_url为simple html dom对象 35 | * @return object simple html dom 对象 36 | */ 37 | public function parser() 38 | { 39 | if (empty($this->dom)) { 40 | $r = Request::get($this->url); 41 | $this->dom = str_get_html($r); 42 | } 43 | } 44 | 45 | /** 46 | * 获取 URL 47 | * @return string URL 48 | */ 49 | public function url() 50 | { 51 | return $this->url; 52 | } 53 | 54 | /** 55 | * 获取该回答的内部ID 56 | * @return string Answer ID 57 | */ 58 | public function aid() 59 | { 60 | if(empty($this->aid)) { 61 | $this->parser(); 62 | $this->aid = $this->dom->find('div.zm-item-answer', 0)->attr['data-aid']; 63 | } 64 | return $this->aid; 65 | } 66 | 67 | /** 68 | * 获取答案所在问题 69 | * @return object Question对象 70 | */ 71 | public function question() 72 | { 73 | if(empty($this->question)) { 74 | $this->parser(); 75 | $question_link = $this->dom->find('div#zh-question-title a',0); 76 | $question_url = ZHIHU_URL.$question_link->href; 77 | $title = $question_link->plaintext; 78 | $this->question = new Question($question_url, $title); 79 | } 80 | return $this->question; 81 | } 82 | 83 | /** 84 | * 获取回答作者 85 | * @return object User对象 86 | */ 87 | public function author() 88 | { 89 | if (empty($this->author)) { 90 | $this->parser(); 91 | $author_link = $this->dom->find('h2.zm-list-content-title', 0); 92 | 93 | $author_name = $author_link->plaintext; 94 | if ($author_name != '匿名用户') { 95 | $author_url = $author_link->find('a', 0)->href; 96 | } else { 97 | $author_url = null; 98 | } 99 | $this->author = new User($author_url, $author_name); 100 | } 101 | return $this->author; 102 | } 103 | 104 | /** 105 | * 获取答案内容 106 | * @return string 答案内容 107 | */ 108 | public function content() 109 | { 110 | if (empty($this->content)) { 111 | $this->parser(); 112 | $this->content = trim($this->dom->find('div#zh-question-answer-wrap div.zm-editable-content', 0)->plaintext); 113 | } 114 | return $this->content; 115 | } 116 | 117 | /** 118 | * 获取回答赞同数 119 | * @return string 赞同数 120 | */ 121 | public function upvote() 122 | { 123 | if (empty($this->upvote)) { 124 | $this->parser(); 125 | $upvote_link = $this->dom->find('div#zh-question-answer-wrap', 0); 126 | 127 | if ( ! empty($upvote_link->find('button.up span', 0))) { 128 | $this->upvote = $upvote_link->find('button.up span', 0)->plaintext; 129 | } else { 130 | $this->upvote = $this->dom->find('div.zm-item-vote a', 0)->plaintext; 131 | } 132 | } 133 | return (int)$this->upvote; 134 | } 135 | 136 | /** 137 | * 获取点赞该回答的用户 138 | * @return Generator 点赞用户迭代器 139 | */ 140 | public function voters() 141 | { 142 | $answer_id = $this->aid(); 143 | $voters_url = ANSWERS_PREFIX_URL.$answer_id.VOTERS_SUFFIX_URL; 144 | while (true) { 145 | $r = Request::get($voters_url); 146 | $response_json = json_decode($r); 147 | if ( ! empty($response_json->paging->next)) { 148 | $voters_url = ZHIHU_URL.$response_json->paging->next; 149 | $voters = $response_json->payload; 150 | foreach ($voters as $voter) { 151 | $dom = str_get_html($voter); 152 | $voter_link = $dom->find('div.body', 0); 153 | yield parser_user($voter_link); 154 | } 155 | } else { 156 | break; 157 | } 158 | } 159 | } 160 | 161 | /** 162 | * 获取该答案被收藏数 163 | * @return integer 收藏数 164 | */ 165 | public function collections_num() 166 | { 167 | $this->parser(); 168 | if ( ! empty($collections_num = $this->dom->find('div.zm-side-section-inner h3 a', 0))) { 169 | $collections_num = (int)$collections_num->plaintext; 170 | } else { 171 | $collections_num = 0; 172 | } 173 | return $collections_num; 174 | } 175 | 176 | /** 177 | * 获取收藏该回答的收藏夹列表 178 | * @return Generator 收藏夹列表 179 | */ 180 | public function collections() 181 | { 182 | $collections_num = $this->collections_num(); 183 | if ($collections_num == 0) { 184 | yield null; 185 | } else { 186 | $collection_url = $this->url.COLLECTION_SUFFIX_URL; 187 | $r = Request::get($collection_url); 188 | $dom = str_get_html($r); 189 | 190 | $_xsrf = _xsrf($dom); 191 | $json = $dom->find('div.zh-general-list', 0)->attr['data-init']; 192 | 193 | for ($i = 0; $i < $collections_num / 20; $i++) { 194 | if ($i == 0) { 195 | for ($j = 0; $j < min($collections_num, 20); $j++) { 196 | $collection_link = $dom->find('div.zm-item', $j); 197 | yield parser_collection_from_answer($collection_link); 198 | } 199 | } else { 200 | $post_url = COLLECTION_LIST_URL; 201 | $params = json_decode(html_entity_decode($json))->params; 202 | $params->offset = $i * 20; 203 | $params = json_encode($params); 204 | 205 | $data = array( 206 | 'method' => 'next', 207 | 'params' => $params, 208 | '_xsrf' => $_xsrf 209 | ); 210 | 211 | $r = Request::post($post_url, $data, array("Referer: {$collection_url}")); 212 | $r = json_decode($r)->msg; 213 | 214 | for ($j = 0; $j < count($r); $j++) { 215 | $dom = str_get_html($r[$j]); 216 | if ( ! empty($dom)) { 217 | $collection_link = $dom->find('div.zm-item', 0); 218 | 219 | yield parser_collection_from_answer($collection_link); 220 | } 221 | } 222 | } 223 | } 224 | } 225 | } 226 | 227 | /** 228 | * 获取问题被浏览次数 229 | * @return integer 浏览数 230 | */ 231 | public function visit_times() 232 | { 233 | $this->parser(); 234 | $visit_times = (int)$this->dom->find('div.zh-answer-status p strong', 0)->plaintext; 235 | return $visit_times; 236 | } 237 | } 238 | -------------------------------------------------------------------------------- /zhihu/collection.php: -------------------------------------------------------------------------------- 1 | url = $url; 19 | if ( ! empty($title)) { 20 | $this->title = $title; 21 | } 22 | if ( ! empty($author)) { 23 | $this->author = $author; 24 | } 25 | } 26 | } 27 | 28 | /** 29 | * 解析收藏主页 30 | * @return object simple html dom 对象 31 | */ 32 | public function parser() 33 | { 34 | if (empty($this->dom)) { 35 | $r = Request::get($this->url); 36 | $this->dom = str_get_html($r); 37 | } 38 | } 39 | 40 | /** 41 | * 获取 URL 42 | * @return string URL 43 | */ 44 | public function url() 45 | { 46 | return $this->url; 47 | } 48 | 49 | /** 50 | * 获取收藏夹名称 51 | * @return string 收藏夹名称 52 | */ 53 | public function title() 54 | { 55 | if( ! empty($this->title)) { 56 | $title = $this->title; 57 | } else { 58 | $this->parser(); 59 | $title = trim($this->dom->find('div#zh-list-title', 0)->plaintext); 60 | } 61 | return $title; 62 | } 63 | 64 | /** 65 | * 获取收藏夹描述 66 | * @return string 收藏夹描述 67 | */ 68 | public function desc() 69 | { 70 | $this->parser(); 71 | $description = $this->dom->find('div#zh-fav-head-description', 0)->plaintext; 72 | return $description; 73 | } 74 | 75 | /** 76 | * 获取收藏夹创建者 77 | * @return object 创建者 78 | */ 79 | public function author() 80 | { 81 | if( ! empty($this->author)) { 82 | $author = $this->author; 83 | } else { 84 | $this->parser(); 85 | $author_link = $this->dom->find('h2.zm-list-content-title', 0); 86 | $author = parser_user($author_link); 87 | } 88 | return $author; 89 | } 90 | 91 | 92 | /** 93 | * 获取收藏夹中的全部回答 94 | * @return Generator 回答迭代器 95 | */ 96 | public function answers() 97 | { 98 | # TODO 99 | $this->parser(); 100 | $max_page = (int)$this->dom->find('div.zm-invite-pager span', -2)->plaintext; 101 | for ($i = 1; $i <= $max_page; $i++) { 102 | $page_url = $this->url.GET_PAGE_SUFFIX_URL.$i; 103 | $r = Request::get($page_url); 104 | $dom = str_get_html($r); 105 | 106 | for ($j = 0; ! empty($dom->find('div.zm-item', $j)); $j++) { 107 | $collection_link = $dom->find('div.zm-item', $j); 108 | if ( ! empty($question_link = $collection_link->find('h2.zm-item-title a', 0))) { 109 | $question_url = ZHIHU_URL.$question_link->href; 110 | $question_title = $question_link->plaintext; 111 | 112 | $question = new Question($question_url, $question_title); 113 | } 114 | 115 | $answer_id = $collection_link->find('div.zm-item-answer', 0)->attr['data-atoken']; 116 | $answer_url = $question_url.'/answer'.$answer_id; 117 | 118 | if ( ! empty($author_link = $collection_link->find('div.zm-item-answer-author-info a', 0))) { 119 | $author_url = ZHIHU_URL.$author_link->href; 120 | $author_name = $author_link->plaintext; 121 | } else { 122 | $author_url = null; 123 | } 124 | $author = new User($author_url, $author_name); 125 | 126 | $upvote = $collection_link->find('div.zm-item-vote a', 0)->plaintext; 127 | 128 | if ( ! empty($content = $collection_link->find('textarea.content', 0))) { 129 | $content = $content->plaintext; 130 | } else { 131 | $content = trim($collection_link->find('div.answer-status p', 0)->plaintext); 132 | } 133 | yield new Answer($answer_url, $question, $author, $upvote, $content); 134 | } 135 | } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /zhihu/libraries/request.php: -------------------------------------------------------------------------------- 1 | size is the "real" number of bytes the dom was created from. 17 | * but for most purposes, it's a really good estimation. 18 | * Paperg - Added the forceTagsClosed to the dom constructor. Forcing tags closed is great for malformed html, but it CAN lead to parsing errors. 19 | * Allow the user to tell us how much they trust the html. 20 | * Paperg add the text and plaintext to the selectors for the find syntax. plaintext implies text in the innertext of a node. text implies that the tag is a text node. 21 | * This allows for us to find tags based on the text they contain. 22 | * Create find_ancestor_tag to see if a tag is - at any level - inside of another specific tag. 23 | * Paperg: added parse_charset so that we know about the character set of the source document. 24 | * NOTE: If the user's system has a routine called get_last_retrieve_url_contents_content_type availalbe, we will assume it's returning the content-type header from the 25 | * last transfer or curl_exec, and we will parse that and use it in preference to any other method of charset detection. 26 | * 27 | * Found infinite loop in the case of broken html in restore_noise. Rewrote to protect from that. 28 | * PaperG (John Schlick) Added get_display_size for "IMG" tags. 29 | * 30 | * Licensed under The MIT License 31 | * Redistributions of files must retain the above copyright notice. 32 | * 33 | * @author S.C. Chen 34 | * @author John Schlick 35 | * @author Rus Carroll 36 | * @version 1.5 ($Rev: 196 $) 37 | * @package PlaceLocalInclude 38 | * @subpackage simple_html_dom 39 | */ 40 | 41 | /** 42 | * All of the Defines for the classes below. 43 | * @author S.C. Chen 44 | */ 45 | define('HDOM_TYPE_ELEMENT', 1); 46 | define('HDOM_TYPE_COMMENT', 2); 47 | define('HDOM_TYPE_TEXT', 3); 48 | define('HDOM_TYPE_ENDTAG', 4); 49 | define('HDOM_TYPE_ROOT', 5); 50 | define('HDOM_TYPE_UNKNOWN', 6); 51 | define('HDOM_QUOTE_DOUBLE', 0); 52 | define('HDOM_QUOTE_SINGLE', 1); 53 | define('HDOM_QUOTE_NO', 3); 54 | define('HDOM_INFO_BEGIN', 0); 55 | define('HDOM_INFO_END', 1); 56 | define('HDOM_INFO_QUOTE', 2); 57 | define('HDOM_INFO_SPACE', 3); 58 | define('HDOM_INFO_TEXT', 4); 59 | define('HDOM_INFO_INNER', 5); 60 | define('HDOM_INFO_OUTER', 6); 61 | define('HDOM_INFO_ENDSPACE',7); 62 | define('DEFAULT_TARGET_CHARSET', 'UTF-8'); 63 | define('DEFAULT_BR_TEXT', "\r\n"); 64 | define('DEFAULT_SPAN_TEXT', " "); 65 | if (!defined('MAX_FILE_SIZE')) { 66 | define('MAX_FILE_SIZE', 600000); 67 | } 68 | 69 | // helper functions 70 | // ----------------------------------------------------------------------------- 71 | // get html dom from file 72 | // $maxlen is defined in the code as PHP_STREAM_COPY_ALL which is defined as -1. 73 | function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) 74 | { 75 | // We DO force the tags to be terminated. 76 | $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText); 77 | // For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done. 78 | $contents = file_get_contents($url, $use_include_path, $context, $offset); 79 | // Paperg - use our own mechanism for getting the contents as we want to control the timeout. 80 | //$contents = retrieve_url_contents($url); 81 | if (empty($contents) || strlen($contents) > MAX_FILE_SIZE) 82 | { 83 | return false; 84 | } 85 | // The second parameter can force the selectors to all be lowercase. 86 | $dom->load($contents, $lowercase, $stripRN); 87 | return $dom; 88 | } 89 | 90 | // get html dom from string 91 | function str_get_html($str, $lowercase=true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) 92 | { 93 | $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText); 94 | if (empty($str) || strlen($str) > MAX_FILE_SIZE) 95 | { 96 | $dom->clear(); 97 | return false; 98 | } 99 | $dom->load($str, $lowercase, $stripRN); 100 | return $dom; 101 | } 102 | 103 | // dump html dom tree 104 | function dump_html_tree($node, $show_attr=true, $deep=0) 105 | { 106 | $node->dump($node); 107 | } 108 | 109 | 110 | /** 111 | * simple html dom node 112 | * PaperG - added ability for "find" routine to lowercase the value of the selector. 113 | * PaperG - added $tag_start to track the start position of the tag in the total byte index 114 | * 115 | * @package PlaceLocalInclude 116 | */ 117 | class simple_html_dom_node 118 | { 119 | public $nodetype = HDOM_TYPE_TEXT; 120 | public $tag = 'text'; 121 | public $attr = array(); 122 | public $children = array(); 123 | public $nodes = array(); 124 | public $parent = null; 125 | // The "info" array - see HDOM_INFO_... for what each element contains. 126 | public $_ = array(); 127 | public $tag_start = 0; 128 | private $dom = null; 129 | 130 | function __construct($dom) 131 | { 132 | $this->dom = $dom; 133 | $dom->nodes[] = $this; 134 | } 135 | 136 | function __destruct() 137 | { 138 | $this->clear(); 139 | } 140 | 141 | function __toString() 142 | { 143 | return $this->outertext(); 144 | } 145 | 146 | // clean up memory due to php5 circular references memory leak... 147 | function clear() 148 | { 149 | $this->dom = null; 150 | $this->nodes = null; 151 | $this->parent = null; 152 | $this->children = null; 153 | } 154 | 155 | // dump node's tree 156 | function dump($show_attr=true, $deep=0) 157 | { 158 | $lead = str_repeat(' ', $deep); 159 | 160 | echo $lead.$this->tag; 161 | if ($show_attr && count($this->attr)>0) 162 | { 163 | echo '('; 164 | foreach ($this->attr as $k=>$v) 165 | echo "[$k]=>\"".$this->$k.'", '; 166 | echo ')'; 167 | } 168 | echo "\n"; 169 | 170 | if ($this->nodes) 171 | { 172 | foreach ($this->nodes as $c) 173 | { 174 | $c->dump($show_attr, $deep+1); 175 | } 176 | } 177 | } 178 | 179 | 180 | // Debugging function to dump a single dom node with a bunch of information about it. 181 | function dump_node($echo=true) 182 | { 183 | 184 | $string = $this->tag; 185 | if (count($this->attr)>0) 186 | { 187 | $string .= '('; 188 | foreach ($this->attr as $k=>$v) 189 | { 190 | $string .= "[$k]=>\"".$this->$k.'", '; 191 | } 192 | $string .= ')'; 193 | } 194 | if (count($this->_)>0) 195 | { 196 | $string .= ' $_ ('; 197 | foreach ($this->_ as $k=>$v) 198 | { 199 | if (is_array($v)) 200 | { 201 | $string .= "[$k]=>("; 202 | foreach ($v as $k2=>$v2) 203 | { 204 | $string .= "[$k2]=>\"".$v2.'", '; 205 | } 206 | $string .= ")"; 207 | } else { 208 | $string .= "[$k]=>\"".$v.'", '; 209 | } 210 | } 211 | $string .= ")"; 212 | } 213 | 214 | if (isset($this->text)) 215 | { 216 | $string .= " text: (" . $this->text . ")"; 217 | } 218 | 219 | $string .= " HDOM_INNER_INFO: '"; 220 | if (isset($node->_[HDOM_INFO_INNER])) 221 | { 222 | $string .= $node->_[HDOM_INFO_INNER] . "'"; 223 | } 224 | else 225 | { 226 | $string .= ' NULL '; 227 | } 228 | 229 | $string .= " children: " . count($this->children); 230 | $string .= " nodes: " . count($this->nodes); 231 | $string .= " tag_start: " . $this->tag_start; 232 | $string .= "\n"; 233 | 234 | if ($echo) 235 | { 236 | echo $string; 237 | return; 238 | } 239 | else 240 | { 241 | return $string; 242 | } 243 | } 244 | 245 | // returns the parent of node 246 | // If a node is passed in, it will reset the parent of the current node to that one. 247 | function parent($parent=null) 248 | { 249 | // I am SURE that this doesn't work properly. 250 | // It fails to unset the current node from it's current parents nodes or children list first. 251 | if ($parent !== null) 252 | { 253 | $this->parent = $parent; 254 | $this->parent->nodes[] = $this; 255 | $this->parent->children[] = $this; 256 | } 257 | 258 | return $this->parent; 259 | } 260 | 261 | // verify that node has children 262 | function has_child() 263 | { 264 | return !empty($this->children); 265 | } 266 | 267 | // returns children of node 268 | function children($idx=-1) 269 | { 270 | if ($idx===-1) 271 | { 272 | return $this->children; 273 | } 274 | if (isset($this->children[$idx])) return $this->children[$idx]; 275 | return null; 276 | } 277 | 278 | // returns the first child of node 279 | function first_child() 280 | { 281 | if (count($this->children)>0) 282 | { 283 | return $this->children[0]; 284 | } 285 | return null; 286 | } 287 | 288 | // returns the last child of node 289 | function last_child() 290 | { 291 | if (($count=count($this->children))>0) 292 | { 293 | return $this->children[$count-1]; 294 | } 295 | return null; 296 | } 297 | 298 | // returns the next sibling of node 299 | function next_sibling() 300 | { 301 | if ($this->parent===null) 302 | { 303 | return null; 304 | } 305 | 306 | $idx = 0; 307 | $count = count($this->parent->children); 308 | while ($idx<$count && $this!==$this->parent->children[$idx]) 309 | { 310 | ++$idx; 311 | } 312 | if (++$idx>=$count) 313 | { 314 | return null; 315 | } 316 | return $this->parent->children[$idx]; 317 | } 318 | 319 | // returns the previous sibling of node 320 | function prev_sibling() 321 | { 322 | if ($this->parent===null) return null; 323 | $idx = 0; 324 | $count = count($this->parent->children); 325 | while ($idx<$count && $this!==$this->parent->children[$idx]) 326 | ++$idx; 327 | if (--$idx<0) return null; 328 | return $this->parent->children[$idx]; 329 | } 330 | 331 | // function to locate a specific ancestor tag in the path to the root. 332 | function find_ancestor_tag($tag) 333 | { 334 | global $debugObject; 335 | if (is_object($debugObject)) { $debugObject->debugLogEntry(1); } 336 | 337 | // Start by including ourselves in the comparison. 338 | $returnDom = $this; 339 | 340 | while (!is_null($returnDom)) 341 | { 342 | if (is_object($debugObject)) { $debugObject->debugLog(2, "Current tag is: " . $returnDom->tag); } 343 | 344 | if ($returnDom->tag == $tag) 345 | { 346 | break; 347 | } 348 | $returnDom = $returnDom->parent; 349 | } 350 | return $returnDom; 351 | } 352 | 353 | // get dom node's inner html 354 | function innertext() 355 | { 356 | if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER]; 357 | if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); 358 | 359 | $ret = ''; 360 | foreach ($this->nodes as $n) 361 | $ret .= $n->outertext(); 362 | return $ret; 363 | } 364 | 365 | // get dom node's outer text (with tag) 366 | function outertext() 367 | { 368 | global $debugObject; 369 | if (is_object($debugObject)) 370 | { 371 | $text = ''; 372 | if ($this->tag == 'text') 373 | { 374 | if (!empty($this->text)) 375 | { 376 | $text = " with text: " . $this->text; 377 | } 378 | } 379 | $debugObject->debugLog(1, 'Innertext of tag: ' . $this->tag . $text); 380 | } 381 | 382 | if ($this->tag==='root') return $this->innertext(); 383 | 384 | // trigger callback 385 | if ($this->dom && $this->dom->callback!==null) 386 | { 387 | call_user_func_array($this->dom->callback, array($this)); 388 | } 389 | 390 | if (isset($this->_[HDOM_INFO_OUTER])) return $this->_[HDOM_INFO_OUTER]; 391 | if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); 392 | 393 | // render begin tag 394 | if ($this->dom && $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]) 395 | { 396 | $ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup(); 397 | } else { 398 | $ret = ""; 399 | } 400 | 401 | // render inner text 402 | if (isset($this->_[HDOM_INFO_INNER])) 403 | { 404 | // If it's a br tag... don't return the HDOM_INNER_INFO that we may or may not have added. 405 | if ($this->tag != "br") 406 | { 407 | $ret .= $this->_[HDOM_INFO_INNER]; 408 | } 409 | } else { 410 | if ($this->nodes) 411 | { 412 | foreach ($this->nodes as $n) 413 | { 414 | $ret .= $this->convert_text($n->outertext()); 415 | } 416 | } 417 | } 418 | 419 | // render end tag 420 | if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0) 421 | $ret .= 'tag.'>'; 422 | return $ret; 423 | } 424 | 425 | // get dom node's plain text 426 | function text() 427 | { 428 | if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER]; 429 | switch ($this->nodetype) 430 | { 431 | case HDOM_TYPE_TEXT: return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); 432 | case HDOM_TYPE_COMMENT: return ''; 433 | case HDOM_TYPE_UNKNOWN: return ''; 434 | } 435 | if (strcasecmp($this->tag, 'script')===0) return ''; 436 | if (strcasecmp($this->tag, 'style')===0) return ''; 437 | 438 | $ret = ''; 439 | // In rare cases, (always node type 1 or HDOM_TYPE_ELEMENT - observed for some span tags, and some p tags) $this->nodes is set to NULL. 440 | // NOTE: This indicates that there is a problem where it's set to NULL without a clear happening. 441 | // WHY is this happening? 442 | if (!is_null($this->nodes)) 443 | { 444 | foreach ($this->nodes as $n) 445 | { 446 | $ret .= $this->convert_text($n->text()); 447 | } 448 | 449 | // If this node is a span... add a space at the end of it so multiple spans don't run into each other. This is plaintext after all. 450 | if ($this->tag == "span") 451 | { 452 | $ret .= $this->dom->default_span_text; 453 | } 454 | 455 | 456 | } 457 | return $ret; 458 | } 459 | 460 | function xmltext() 461 | { 462 | $ret = $this->innertext(); 463 | $ret = str_ireplace('', '', $ret); 465 | return $ret; 466 | } 467 | 468 | // build node's text with tag 469 | function makeup() 470 | { 471 | // text, comment, unknown 472 | if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); 473 | 474 | $ret = '<'.$this->tag; 475 | $i = -1; 476 | 477 | foreach ($this->attr as $key=>$val) 478 | { 479 | ++$i; 480 | 481 | // skip removed attribute 482 | if ($val===null || $val===false) 483 | continue; 484 | 485 | $ret .= $this->_[HDOM_INFO_SPACE][$i][0]; 486 | //no value attr: nowrap, checked selected... 487 | if ($val===true) 488 | $ret .= $key; 489 | else { 490 | switch ($this->_[HDOM_INFO_QUOTE][$i]) 491 | { 492 | case HDOM_QUOTE_DOUBLE: $quote = '"'; break; 493 | case HDOM_QUOTE_SINGLE: $quote = '\''; break; 494 | default: $quote = ''; 495 | } 496 | $ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1].'='.$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote; 497 | } 498 | } 499 | $ret = $this->dom->restore_noise($ret); 500 | return $ret . $this->_[HDOM_INFO_ENDSPACE] . '>'; 501 | } 502 | 503 | // find elements by css selector 504 | //PaperG - added ability for find to lowercase the value of the selector. 505 | function find($selector, $idx=null, $lowercase=false) 506 | { 507 | $selectors = $this->parse_selector($selector); 508 | if (($count=count($selectors))===0) return array(); 509 | $found_keys = array(); 510 | 511 | // find each selector 512 | for ($c=0; $c<$count; ++$c) 513 | { 514 | // The change on the below line was documented on the sourceforge code tracker id 2788009 515 | // used to be: if (($levle=count($selectors[0]))===0) return array(); 516 | if (($levle=count($selectors[$c]))===0) return array(); 517 | if (!isset($this->_[HDOM_INFO_BEGIN])) return array(); 518 | 519 | $head = array($this->_[HDOM_INFO_BEGIN]=>1); 520 | 521 | // handle descendant selectors, no recursive! 522 | for ($l=0; $l<$levle; ++$l) 523 | { 524 | $ret = array(); 525 | foreach ($head as $k=>$v) 526 | { 527 | $n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k]; 528 | //PaperG - Pass this optional parameter on to the seek function. 529 | $n->seek($selectors[$c][$l], $ret, $lowercase); 530 | } 531 | $head = $ret; 532 | } 533 | 534 | foreach ($head as $k=>$v) 535 | { 536 | if (!isset($found_keys[$k])) 537 | $found_keys[$k] = 1; 538 | } 539 | } 540 | 541 | // sort keys 542 | ksort($found_keys); 543 | 544 | $found = array(); 545 | foreach ($found_keys as $k=>$v) 546 | $found[] = $this->dom->nodes[$k]; 547 | 548 | // return nth-element or array 549 | if (is_null($idx)) return $found; 550 | else if ($idx<0) $idx = count($found) + $idx; 551 | return (isset($found[$idx])) ? $found[$idx] : null; 552 | } 553 | 554 | // seek for given conditions 555 | // PaperG - added parameter to allow for case insensitive testing of the value of a selector. 556 | protected function seek($selector, &$ret, $lowercase=false) 557 | { 558 | global $debugObject; 559 | if (is_object($debugObject)) { $debugObject->debugLogEntry(1); } 560 | 561 | list($tag, $key, $val, $exp, $no_key) = $selector; 562 | 563 | // xpath index 564 | if ($tag && $key && is_numeric($key)) 565 | { 566 | $count = 0; 567 | foreach ($this->children as $c) 568 | { 569 | if ($tag==='*' || $tag===$c->tag) { 570 | if (++$count==$key) { 571 | $ret[$c->_[HDOM_INFO_BEGIN]] = 1; 572 | return; 573 | } 574 | } 575 | } 576 | return; 577 | } 578 | 579 | $end = (!empty($this->_[HDOM_INFO_END])) ? $this->_[HDOM_INFO_END] : 0; 580 | if ($end==0) { 581 | $parent = $this->parent; 582 | while (!isset($parent->_[HDOM_INFO_END]) && $parent!==null) { 583 | $end -= 1; 584 | $parent = $parent->parent; 585 | } 586 | $end += $parent->_[HDOM_INFO_END]; 587 | } 588 | 589 | for ($i=$this->_[HDOM_INFO_BEGIN]+1; $i<$end; ++$i) { 590 | $node = $this->dom->nodes[$i]; 591 | 592 | $pass = true; 593 | 594 | if ($tag==='*' && !$key) { 595 | if (in_array($node, $this->children, true)) 596 | $ret[$i] = 1; 597 | continue; 598 | } 599 | 600 | // compare tag 601 | if ($tag && $tag!=$node->tag && $tag!=='*') {$pass=false;} 602 | // compare key 603 | if ($pass && $key) { 604 | if ($no_key) { 605 | if (isset($node->attr[$key])) $pass=false; 606 | } else { 607 | if (($key != "plaintext") && !isset($node->attr[$key])) $pass=false; 608 | } 609 | } 610 | // compare value 611 | if ($pass && $key && $val && $val!=='*') { 612 | // If they have told us that this is a "plaintext" search then we want the plaintext of the node - right? 613 | if ($key == "plaintext") { 614 | // $node->plaintext actually returns $node->text(); 615 | $nodeKeyValue = $node->text(); 616 | } else { 617 | // this is a normal search, we want the value of that attribute of the tag. 618 | $nodeKeyValue = $node->attr[$key]; 619 | } 620 | if (is_object($debugObject)) {$debugObject->debugLog(2, "testing node: " . $node->tag . " for attribute: " . $key . $exp . $val . " where nodes value is: " . $nodeKeyValue);} 621 | 622 | //PaperG - If lowercase is set, do a case insensitive test of the value of the selector. 623 | if ($lowercase) { 624 | $check = $this->match($exp, strtolower($val), strtolower($nodeKeyValue)); 625 | } else { 626 | $check = $this->match($exp, $val, $nodeKeyValue); 627 | } 628 | if (is_object($debugObject)) {$debugObject->debugLog(2, "after match: " . ($check ? "true" : "false"));} 629 | 630 | // handle multiple class 631 | if (!$check && strcasecmp($key, 'class')===0) { 632 | foreach (explode(' ',$node->attr[$key]) as $k) { 633 | // Without this, there were cases where leading, trailing, or double spaces lead to our comparing blanks - bad form. 634 | if (!empty($k)) { 635 | if ($lowercase) { 636 | $check = $this->match($exp, strtolower($val), strtolower($k)); 637 | } else { 638 | $check = $this->match($exp, $val, $k); 639 | } 640 | if ($check) break; 641 | } 642 | } 643 | } 644 | if (!$check) $pass = false; 645 | } 646 | if ($pass) $ret[$i] = 1; 647 | unset($node); 648 | } 649 | // It's passed by reference so this is actually what this function returns. 650 | if (is_object($debugObject)) {$debugObject->debugLog(1, "EXIT - ret: ", $ret);} 651 | } 652 | 653 | protected function match($exp, $pattern, $value) { 654 | global $debugObject; 655 | if (is_object($debugObject)) {$debugObject->debugLogEntry(1);} 656 | 657 | switch ($exp) { 658 | case '=': 659 | return ($value===$pattern); 660 | case '!=': 661 | return ($value!==$pattern); 662 | case '^=': 663 | return preg_match("/^".preg_quote($pattern,'/')."/", $value); 664 | case '$=': 665 | return preg_match("/".preg_quote($pattern,'/')."$/", $value); 666 | case '*=': 667 | if ($pattern[0]=='/') { 668 | return preg_match($pattern, $value); 669 | } 670 | return preg_match("/".$pattern."/i", $value); 671 | } 672 | return false; 673 | } 674 | 675 | protected function parse_selector($selector_string) { 676 | global $debugObject; 677 | if (is_object($debugObject)) {$debugObject->debugLogEntry(1);} 678 | 679 | // pattern of CSS selectors, modified from mootools 680 | // Paperg: Add the colon to the attrbute, so that it properly finds like google does. 681 | // Note: if you try to look at this attribute, yo MUST use getAttribute since $dom->x:y will fail the php syntax check. 682 | // Notice the \[ starting the attbute? and the @? following? This implies that an attribute can begin with an @ sign that is not captured. 683 | // This implies that an html attribute specifier may start with an @ sign that is NOT captured by the expression. 684 | // farther study is required to determine of this should be documented or removed. 685 | // $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; 686 | $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-:]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; 687 | preg_match_all($pattern, trim($selector_string).' ', $matches, PREG_SET_ORDER); 688 | if (is_object($debugObject)) {$debugObject->debugLog(2, "Matches Array: ", $matches);} 689 | 690 | $selectors = array(); 691 | $result = array(); 692 | //print_r($matches); 693 | 694 | foreach ($matches as $m) { 695 | $m[0] = trim($m[0]); 696 | if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') continue; 697 | // for browser generated xpath 698 | if ($m[1]==='tbody') continue; 699 | 700 | list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false); 701 | if (!empty($m[2])) {$key='id'; $val=$m[2];} 702 | if (!empty($m[3])) {$key='class'; $val=$m[3];} 703 | if (!empty($m[4])) {$key=$m[4];} 704 | if (!empty($m[5])) {$exp=$m[5];} 705 | if (!empty($m[6])) {$val=$m[6];} 706 | 707 | // convert to lowercase 708 | if ($this->dom->lowercase) {$tag=strtolower($tag); $key=strtolower($key);} 709 | //elements that do NOT have the specified attribute 710 | if (isset($key[0]) && $key[0]==='!') {$key=substr($key, 1); $no_key=true;} 711 | 712 | $result[] = array($tag, $key, $val, $exp, $no_key); 713 | if (trim($m[7])===',') { 714 | $selectors[] = $result; 715 | $result = array(); 716 | } 717 | } 718 | if (count($result)>0) 719 | $selectors[] = $result; 720 | return $selectors; 721 | } 722 | 723 | function __get($name) { 724 | if (isset($this->attr[$name])) 725 | { 726 | return $this->convert_text($this->attr[$name]); 727 | } 728 | switch ($name) { 729 | case 'outertext': return $this->outertext(); 730 | case 'innertext': return $this->innertext(); 731 | case 'plaintext': return $this->text(); 732 | case 'xmltext': return $this->xmltext(); 733 | default: return array_key_exists($name, $this->attr); 734 | } 735 | } 736 | 737 | function __set($name, $value) { 738 | switch ($name) { 739 | case 'outertext': return $this->_[HDOM_INFO_OUTER] = $value; 740 | case 'innertext': 741 | if (isset($this->_[HDOM_INFO_TEXT])) return $this->_[HDOM_INFO_TEXT] = $value; 742 | return $this->_[HDOM_INFO_INNER] = $value; 743 | } 744 | if (!isset($this->attr[$name])) { 745 | $this->_[HDOM_INFO_SPACE][] = array(' ', '', ''); 746 | $this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE; 747 | } 748 | $this->attr[$name] = $value; 749 | } 750 | 751 | function __isset($name) { 752 | switch ($name) { 753 | case 'outertext': return true; 754 | case 'innertext': return true; 755 | case 'plaintext': return true; 756 | } 757 | //no value attr: nowrap, checked selected... 758 | return (array_key_exists($name, $this->attr)) ? true : isset($this->attr[$name]); 759 | } 760 | 761 | function __unset($name) { 762 | if (isset($this->attr[$name])) 763 | unset($this->attr[$name]); 764 | } 765 | 766 | // PaperG - Function to convert the text from one character set to another if the two sets are not the same. 767 | function convert_text($text) 768 | { 769 | global $debugObject; 770 | if (is_object($debugObject)) {$debugObject->debugLogEntry(1);} 771 | 772 | $converted_text = $text; 773 | 774 | $sourceCharset = ""; 775 | $targetCharset = ""; 776 | 777 | if ($this->dom) 778 | { 779 | $sourceCharset = strtoupper($this->dom->_charset); 780 | $targetCharset = strtoupper($this->dom->_target_charset); 781 | } 782 | if (is_object($debugObject)) {$debugObject->debugLog(3, "source charset: " . $sourceCharset . " target charaset: " . $targetCharset);} 783 | 784 | if (!empty($sourceCharset) && !empty($targetCharset) && (strcasecmp($sourceCharset, $targetCharset) != 0)) 785 | { 786 | // Check if the reported encoding could have been incorrect and the text is actually already UTF-8 787 | if ((strcasecmp($targetCharset, 'UTF-8') == 0) && ($this->is_utf8($text))) 788 | { 789 | $converted_text = $text; 790 | } 791 | else 792 | { 793 | $converted_text = iconv($sourceCharset, $targetCharset, $text); 794 | } 795 | } 796 | 797 | // Lets make sure that we don't have that silly BOM issue with any of the utf-8 text we output. 798 | if ($targetCharset == 'UTF-8') 799 | { 800 | if (substr($converted_text, 0, 3) == "\xef\xbb\xbf") 801 | { 802 | $converted_text = substr($converted_text, 3); 803 | } 804 | if (substr($converted_text, -3) == "\xef\xbb\xbf") 805 | { 806 | $converted_text = substr($converted_text, 0, -3); 807 | } 808 | } 809 | 810 | return $converted_text; 811 | } 812 | 813 | /** 814 | * Returns true if $string is valid UTF-8 and false otherwise. 815 | * 816 | * @param mixed $str String to be tested 817 | * @return boolean 818 | */ 819 | static function is_utf8($str) 820 | { 821 | $c=0; $b=0; 822 | $bits=0; 823 | $len=strlen($str); 824 | for($i=0; $i<$len; $i++) 825 | { 826 | $c=ord($str[$i]); 827 | if($c > 128) 828 | { 829 | if(($c >= 254)) return false; 830 | elseif($c >= 252) $bits=6; 831 | elseif($c >= 248) $bits=5; 832 | elseif($c >= 240) $bits=4; 833 | elseif($c >= 224) $bits=3; 834 | elseif($c >= 192) $bits=2; 835 | else return false; 836 | if(($i+$bits) > $len) return false; 837 | while($bits > 1) 838 | { 839 | $i++; 840 | $b=ord($str[$i]); 841 | if($b < 128 || $b > 191) return false; 842 | $bits--; 843 | } 844 | } 845 | } 846 | return true; 847 | } 848 | /* 849 | function is_utf8($string) 850 | { 851 | //this is buggy 852 | return (utf8_encode(utf8_decode($string)) == $string); 853 | } 854 | */ 855 | 856 | /** 857 | * Function to try a few tricks to determine the displayed size of an img on the page. 858 | * NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types. 859 | * 860 | * @author John Schlick 861 | * @version April 19 2012 862 | * @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out. 863 | */ 864 | function get_display_size() 865 | { 866 | global $debugObject; 867 | 868 | $width = -1; 869 | $height = -1; 870 | 871 | if ($this->tag !== 'img') 872 | { 873 | return false; 874 | } 875 | 876 | // See if there is aheight or width attribute in the tag itself. 877 | if (isset($this->attr['width'])) 878 | { 879 | $width = $this->attr['width']; 880 | } 881 | 882 | if (isset($this->attr['height'])) 883 | { 884 | $height = $this->attr['height']; 885 | } 886 | 887 | // Now look for an inline style. 888 | if (isset($this->attr['style'])) 889 | { 890 | // Thanks to user gnarf from stackoverflow for this regular expression. 891 | $attributes = array(); 892 | preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->attr['style'], $matches, PREG_SET_ORDER); 893 | foreach ($matches as $match) { 894 | $attributes[$match[1]] = $match[2]; 895 | } 896 | 897 | // If there is a width in the style attributes: 898 | if (isset($attributes['width']) && $width == -1) 899 | { 900 | // check that the last two characters are px (pixels) 901 | if (strtolower(substr($attributes['width'], -2)) == 'px') 902 | { 903 | $proposed_width = substr($attributes['width'], 0, -2); 904 | // Now make sure that it's an integer and not something stupid. 905 | if (filter_var($proposed_width, FILTER_VALIDATE_INT)) 906 | { 907 | $width = $proposed_width; 908 | } 909 | } 910 | } 911 | 912 | // If there is a width in the style attributes: 913 | if (isset($attributes['height']) && $height == -1) 914 | { 915 | // check that the last two characters are px (pixels) 916 | if (strtolower(substr($attributes['height'], -2)) == 'px') 917 | { 918 | $proposed_height = substr($attributes['height'], 0, -2); 919 | // Now make sure that it's an integer and not something stupid. 920 | if (filter_var($proposed_height, FILTER_VALIDATE_INT)) 921 | { 922 | $height = $proposed_height; 923 | } 924 | } 925 | } 926 | 927 | } 928 | 929 | // Future enhancement: 930 | // Look in the tag to see if there is a class or id specified that has a height or width attribute to it. 931 | 932 | // Far future enhancement 933 | // Look at all the parent tags of this image to see if they specify a class or id that has an img selector that specifies a height or width 934 | // Note that in this case, the class or id will have the img subselector for it to apply to the image. 935 | 936 | // ridiculously far future development 937 | // If the class or id is specified in a SEPARATE css file thats not on the page, go get it and do what we were just doing for the ones on the page. 938 | 939 | $result = array('height' => $height, 940 | 'width' => $width); 941 | return $result; 942 | } 943 | 944 | // camel naming conventions 945 | function getAllAttributes() {return $this->attr;} 946 | function getAttribute($name) {return $this->__get($name);} 947 | function setAttribute($name, $value) {$this->__set($name, $value);} 948 | function hasAttribute($name) {return $this->__isset($name);} 949 | function removeAttribute($name) {$this->__set($name, null);} 950 | function getElementById($id) {return $this->find("#$id", 0);} 951 | function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);} 952 | function getElementByTagName($name) {return $this->find($name, 0);} 953 | function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);} 954 | function parentNode() {return $this->parent();} 955 | function childNodes($idx=-1) {return $this->children($idx);} 956 | function firstChild() {return $this->first_child();} 957 | function lastChild() {return $this->last_child();} 958 | function nextSibling() {return $this->next_sibling();} 959 | function previousSibling() {return $this->prev_sibling();} 960 | function hasChildNodes() {return $this->has_child();} 961 | function nodeName() {return $this->tag;} 962 | function appendChild($node) {$node->parent($this); return $node;} 963 | 964 | } 965 | 966 | /** 967 | * simple html dom parser 968 | * Paperg - in the find routine: allow us to specify that we want case insensitive testing of the value of the selector. 969 | * Paperg - change $size from protected to public so we can easily access it 970 | * Paperg - added ForceTagsClosed in the constructor which tells us whether we trust the html or not. Default is to NOT trust it. 971 | * 972 | * @package PlaceLocalInclude 973 | */ 974 | class simple_html_dom 975 | { 976 | public $root = null; 977 | public $nodes = array(); 978 | public $callback = null; 979 | public $lowercase = false; 980 | // Used to keep track of how large the text was when we started. 981 | public $original_size; 982 | public $size; 983 | protected $pos; 984 | protected $doc; 985 | protected $char; 986 | protected $cursor; 987 | protected $parent; 988 | protected $noise = array(); 989 | protected $token_blank = " \t\r\n"; 990 | protected $token_equal = ' =/>'; 991 | protected $token_slash = " />\r\n\t"; 992 | protected $token_attr = ' >'; 993 | // Note that this is referenced by a child node, and so it needs to be public for that node to see this information. 994 | public $_charset = ''; 995 | public $_target_charset = ''; 996 | protected $default_br_text = ""; 997 | public $default_span_text = ""; 998 | 999 | // use isset instead of in_array, performance boost about 30%... 1000 | protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1); 1001 | protected $block_tags = array('root'=>1, 'body'=>1, 'form'=>1, 'div'=>1, 'span'=>1, 'table'=>1); 1002 | // Known sourceforge issue #2977341 1003 | // B tags that are not closed cause us to return everything to the end of the document. 1004 | protected $optional_closing_tags = array( 1005 | 'tr'=>array('tr'=>1, 'td'=>1, 'th'=>1), 1006 | 'th'=>array('th'=>1), 1007 | 'td'=>array('td'=>1), 1008 | 'li'=>array('li'=>1), 1009 | 'dt'=>array('dt'=>1, 'dd'=>1), 1010 | 'dd'=>array('dd'=>1, 'dt'=>1), 1011 | 'dl'=>array('dd'=>1, 'dt'=>1), 1012 | 'p'=>array('p'=>1), 1013 | 'nobr'=>array('nobr'=>1), 1014 | 'b'=>array('b'=>1), 1015 | 'option'=>array('option'=>1), 1016 | ); 1017 | 1018 | function __construct($str=null, $lowercase=true, $forceTagsClosed=true, $target_charset=DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) 1019 | { 1020 | if ($str) 1021 | { 1022 | if (preg_match("/^http:\/\//i",$str) || is_file($str)) 1023 | { 1024 | $this->load_file($str); 1025 | } 1026 | else 1027 | { 1028 | $this->load($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText); 1029 | } 1030 | } 1031 | // Forcing tags to be closed implies that we don't trust the html, but it can lead to parsing errors if we SHOULD trust the html. 1032 | if (!$forceTagsClosed) { 1033 | $this->optional_closing_array=array(); 1034 | } 1035 | $this->_target_charset = $target_charset; 1036 | } 1037 | 1038 | function __destruct() 1039 | { 1040 | $this->clear(); 1041 | } 1042 | 1043 | // load html from string 1044 | function load($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) 1045 | { 1046 | global $debugObject; 1047 | 1048 | // prepare 1049 | $this->prepare($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText); 1050 | // strip out comments 1051 | $this->remove_noise("''is"); 1052 | // strip out cdata 1053 | $this->remove_noise("''is", true); 1054 | // Per sourceforge http://sourceforge.net/tracker/?func=detail&aid=2949097&group_id=218559&atid=1044037 1055 | // Script tags removal now preceeds style tag removal. 1056 | // strip out