Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. public function actionNews()
  2. {
  3. $arr = ParseAllNews::newsList();
  4. foreach ($arr as $key => $value){
  5. $model = new ParseAllNews();
  6. $model->img = $arr[$key]["img"];
  7. $model->text = $arr[$key]["text"];
  8. $model->title = $arr[$key]["title"];
  9. $model->url = $arr[$key]["site_url"];
  10. $model->save();
  11. }
  12. }
  13.  
  14. public function actionsOneNews($site_url)
  15. {
  16. $news = Content::OneNews($site_url);
  17. foreach ($news as $key => $value){
  18. $model = new Content();
  19. $model->img = $news[$key]["img"];
  20. $model->text = $news[$key]["text"];
  21. $model->title = $news[$key]["title"];
  22. $model->save();
  23. }
  24. }
  25.  
  26. public static function tableName()
  27. {
  28. return 'news';
  29. }
  30.  
  31. public static function newsList(){
  32. $site = "http://site.ru/";
  33.  
  34. $client = new Client();
  35.  
  36. $res = $client->request('GET', 'http://site.ru');
  37.  
  38. $body = $res->getBody();
  39.  
  40. $document = phpQuery::newDocumentHTML($body);
  41.  
  42. $links = $document->find('.bordered-title');
  43.  
  44. foreach ($links as $link) {
  45. $link = pq($link);
  46. $a = $link->find('a', 0)->attr('href');
  47. $site_url = $site . $a;
  48. $get = $client->request('GET', $site_url);
  49. $body_get = $get->getBody();
  50. $document = phpQuery::newDocumentHTML($body_get);
  51. $links_get = $document->find('.b-longgrid-column .item.article');
  52. $links_get->find('.item__info')->remove();
  53. foreach ($links_get as $link_get){
  54. $img = pq($link_get)->find('img')->attr('src');
  55. $text = pq($link_get)->find('.rightcol')->html();
  56. $title = pq($link_get)->find('.titles a>span')->html();
  57. $url = pq($link_get)->find('.titles h3 a')->attr('href');
  58. $site_url = $site . $url;
  59. $i++;
  60. $mas[$i] = array('title' => $title, 'img' => $img, 'text' => $text, 'site_url' => $site_url, );
  61. }
  62. }
  63. return $mas;
  64. }
  65.  
  66. public static function tableName()
  67. {
  68. return 'content';
  69. }
  70.  
  71. public function getNews()
  72. {
  73. return $this->hasOne(News::className(), ['id' => 'news_id']);
  74. }
  75.  
  76. public static function OneNews($site_url){
  77.  
  78. // создаем экземпляр класса
  79. $client = new Client();
  80. // отправляем запрос к странице Яндекса
  81. $res = $client->request('GET', $site_url);
  82. // получаем данные между открывающим и закрывающим тегами body
  83. $body = $res->getBody();
  84. // подключаем phpQuery
  85. $document = phpQuery::newDocumentHTML($body);
  86. // получаем список новостей
  87. $news = $document->find('.b-topic');
  88. // выполняем проход циклом по списку
  89. foreach ($news as $article) {
  90. //pq аналог $ в jQuery
  91. $article = pq($article);
  92. $img = $article->find('img')->attr('src');
  93. $text = $article->find('.b-text')->html();
  94. $title = $article->find('.b-topic__title')->html();
  95. $rightcol = $article->find('.b-topic__rightcol')->html();
  96. $i++;
  97. $mas[$i] = array('title' => $title, 'img' => $img, 'text' => $text, 'rightcol' => $rightcol);
  98.  
  99. }
  100. return $mas;
  101. }
  102.  
  103. $a = new actionNews();
  104. actionsOneNews($a);
  105.  
  106. class SiteController extends Controller{
  107.  
  108. /*
  109. * В классе контроллера определяешь свойство:
  110. */
  111. private $_siteUrl; // сюда будет записан url
  112.  
  113. public function actionNews(){
  114.  
  115. //....
  116.  
  117. /*
  118. * Записываешь нужное значение в свойство.
  119. */
  120. $this->_siteUrl = $model->url; // или откуда берется адрес
  121.  
  122. //....
  123. }
  124.  
  125. public function actionsOneNews(){
  126.  
  127. // ....
  128. /*
  129. * Используешь полученное значение как душе угодно.
  130. */
  131. if($this->_siteUrl) returt "Hello News!";
  132.  
  133. // ....
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement