Tell me how to pass the $ site_url parameter from public function actionNews to public function actionsOneNews for further content parsing. Parsing the list of news, writing them to the database, then you need to pull out the address of each news and pass it on to further parse the content. Controller:
public function actionNews() { $arr = ParseAllNews::newsList(); foreach ($arr as $key => $value){ $model = new ParseAllNews(); $model->img = $arr[$key]["img"]; $model->text = $arr[$key]["text"]; $model->title = $arr[$key]["title"]; $model->url = $arr[$key]["site_url"]; $model->save(); } } public function actionsOneNews($site_url) { $news = Content::OneNews($site_url); foreach ($news as $key => $value){ $model = new Content(); $model->img = $news[$key]["img"]; $model->text = $news[$key]["text"]; $model->title = $news[$key]["title"]; $model->save(); } } Model ParseAllNews:
public static function tableName() { return 'news'; } public static function newsList(){ $site = "http://site.ru/"; $client = new Client(); $res = $client->request('GET', 'http://site.ru'); $body = $res->getBody(); $document = \phpQuery::newDocumentHTML($body); $links = $document->find('.bordered-title'); foreach ($links as $link) { $link = pq($link); $a = $link->find('a', 0)->attr('href'); $site_url = $site . $a; $get = $client->request('GET', $site_url); $body_get = $get->getBody(); $document = \phpQuery::newDocumentHTML($body_get); $links_get = $document->find('.b-longgrid-column .item.article'); $links_get->find('.item__info')->remove(); foreach ($links_get as $link_get){ $img = pq($link_get)->find('img')->attr('src'); $text = pq($link_get)->find('.rightcol')->html(); $title = pq($link_get)->find('.titles a>span')->html(); $url = pq($link_get)->find('.titles h3 a')->attr('href'); $site_url = $site . $url; $i++; $mas[$i] = array('title' => $title, 'img' => $img, 'text' => $text, 'site_url' => $site_url, ); } } return $mas; } Content Model:
public static function tableName() { return 'content'; } public function getNews() { return $this->hasOne(News::className(), ['id' => 'news_id']); } public static function OneNews($site_url){ // создаем экземпляр класса $client = new Client(); // отправляем запрос к странице Яндекса $res = $client->request('GET', $site_url); // получаем данные между открывающим и закрывающим тегами body $body = $res->getBody(); // подключаем phpQuery $document = \phpQuery::newDocumentHTML($body); // получаем список новостей $news = $document->find('.b-topic'); // выполняем проход циклом по списку foreach ($news as $article) { //pq аналог $ в jQuery $article = pq($article); $img = $article->find('img')->attr('src'); $text = $article->find('.b-text')->html(); $title = $article->find('.b-topic__title')->html(); $rightcol = $article->find('.b-topic__rightcol')->html(); $i++; $mas[$i] = array('title' => $title, 'img' => $img, 'text' => $text, 'rightcol' => $rightcol); } return $mas; }