示例#1
0
 public function actionSimple()
 {
     ini_set('max_execution_time', 0);
     ini_set('memory_limit', '128M');
     ini_set('default_charset', 'UTF-8');
     //header('Content-Type: text/html; charset=UTF-8');
     $arrResult = [];
     $arrResult2 = [];
     $html = new simple_html_dom();
     $html->load_file('http://politrussia.com/news/');
     // Find all links
     foreach ($html->find('a.overlink') as $element) {
         //echo $element->href. '<br>';
         $arrResult[] = $element->href;
     }
     //vd($arrResult);
     $i = 0;
     foreach ($arrResult as $key => $link) {
         $html->load_file('http://politrussia.com' . $link);
         $i++;
         $content = $html->find('div[class="news_text"]', 0)->plaintext;
         $title = $html->find('h1[itemprop="name"]', 0)->plaintext;
         foreach ($html->find('img[itemprop="contentUrl"]') as $element) {
             $img2 = 'http://politrussia.com' . $element->src;
         }
         $content2 = mb_convert_encoding($content, "UTF-8", "Windows-1251");
         $title2 = mb_convert_encoding($title, "UTF-8", "Windows-1251");
         $arrResult2[$key]['title'] = $title2;
         $arrResult2[$key]['content'] = $content2;
         $arrResult2[$key]['img'] = $img2;
     }
     foreach ($arrResult2 as $key => $row) {
         $modelBlog = new Blog();
         $modelBlog->title = $row['title'];
         $modelBlog->content = $row['content'];
         $modelBlog->created_at = time();
         $modelBlog->updated_at = time();
         $modelBlog->author = Yii::$app->user->id;
         $modelBlog->image = $row['img'];
         $dublicate = Blog::getDublicateByTitle($row['title']);
         if (!$dublicate) {
             $modelBlog->save();
         }
     }
     return $this->render('/site/index');
 }