我有这样的功能:
public function handle()
{
News::truncate();
$client = new Client();
$crawler = $client->request('GET', 'features');
$crawler->filter('div[id=content]>.homefeature')->each(function ($node, $key) {
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
if ($image = $node->filter('img')->count () > 0) {
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
$image = $node->filter('img')->image();
$image_s = $image->getUri();
$image_s = preg_replace("/thumb_/", "", $image_s);
$filename = basename($image_s);
$image_path = ('news-gallery/' . $filename);
Image::make($image_s)->save(public_path('news-gallery/' . $filename));
$id = 1+ $key + 1;
$news = News::where('id', $id)->first();
// if news is null
if (!$news) {
$news = new News();
}
$news->title = $title;
$news->datepublished = $datepublished;
$news->description = $description;
$news->link = $link_r;
$news->image = $image_path;
$news->save();
$this->info('Main article ' .$title. ' done succesfully');
});
$crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) {
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
$image = $node->filter('img')->image();
$image_s = $image->getUri();
$image_s = preg_replace("/thumb_/", "", $image_s);
$filename = basename($image_s);
$image_path = ('news-gallery/' . $filename);
Image::make($image_s)->save(public_path('news-gallery/' . $filename));
//$id = 1+ $key + 1;
$newsArray[] = [
'title' => $title,
'datepublished' => $datepublished,
'description' => $description,
'link' => $link_r,
'image' => $image_path,
];
$this->info('Scraping done for ' .$title. ' succesfully');
});
print_r($newsArray);
//$news = News::insert($newsArray);
}
}
所以我试图在保存它之前将所有内容放入数组中,但是我得到一个错误,说$ newsArray是未定义的?如果我把print_r放在函数中,就在它下面我得到一个输出。怎么解决这个问题?我想要做的是像现在一样循环每个结果,将其插入到数组中,然后将其批量插入到db
//例:
public function handle()
{
/* Clear table, set array of id's, and get today's date */
Film::truncate();
$arrayOfCinemaIds = [10565,7311,9434];
$startdate = strtotime("today");
$date = date("Y-m-d", $startdate);
/* Set date to today + + days */
$endate = strtotime("+6 Days");
/* Check if current date is less than $endate */
while ($startdate < $endate) {
/* If it is less, do a foreach loop for $arrayOfCinemaIds */
foreach($arrayOfCinemaIds as $id){
/* Get data from cinelist api */
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/'.$id.'/?day='.$date);
$data = json_decode($data);
/* Find entities that match api_key */
$find_id = Entity::where('api_key', $id)->get();
foreach($find_id as $id_found) {
$entity_id = $id_found->id;
foreach($data->listings as $listing) {
/* Put each record into an array */
$title = $listing->title;
$time = implode(', ', $listing->times);
$films[] = [
'title' => $title,
'times' => $time,
'entity_id' => $entity_id,
'date' => $date,
];
}
}
}
/* Increase $startdate by 1 day, display message, and change $date to new date */
$startdate = strtotime("+1 Days", $startdate);
$this->info('Scraping for ' .$date. ' done, moving to next one');
$date = date("Y-m-d", $startdate);
}
/* Insert array of films to DB, display message */
$film = Film::insert($films);
$this->info('Succesfully Added Films to DB!');
}
我有这个其他工匠的命令,这个工作绝对没问题,为什么上面那个没有呢?
答案 0 :(得分:0)
您已在每个函数中定义了$newsArray
数组,并尝试在函数外部进行访问。因此,遇到此错误。
因此,在each()
函数
$newsArray = array();
然后你可以使用它。
<强>更新强>
在定义数组之后,您可以在array_push()
函数内使用each()
,
$element = [
'title' => $title,
'datepublished' => $datepublished,
'description' => $description,
'link' => $link_r,
'image' => $image_path,
];
array_push($newsArray,$element);
然后您可以使用$newsArray
插入数据。
希望你明白。
答案 1 :(得分:0)
声明数组并使用refrence
$newsArray = []
$crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) use(&newsArray) {
//Your loop code.
});
答案 2 :(得分:0)
我想你可以试试这个:
$newsArray = array();
$crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) {
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
$image = $node->filter('img')->image();
$image_s = $image->getUri();
$image_s = preg_replace("/thumb_/", "", $image_s);
$filename = basename($image_s);
$image_path = ('news-gallery/' . $filename);
Image::make($image_s)->save(public_path('news-gallery/' . $filename));
//$id = 1+ $key + 1;
$newsArray = [
'title' => $title,
'datepublished' => $datepublished,
'description' => $description,
'link' => $link_r,
'image' => $image_path,
];
$this->info('Scraping done for ' .$title. ' succesfully');
});
//print_r($newsArray);
$news = News::insert($newsArray);
希望这对你有用!!!