我有这个代码:
$url = 'http://example.com/523223.json?';
$json= file_get_contents($url);
$data = json_decode($json);
$rows = $data->{'items'};
foreach ($rows as $row) {
echo '<p>';
$title = $row->name;
$description = $row->description;
$link = $row->link;
$image_link = $row->thumbnails;
$path = $image_link->large;
$filename = basename($path);
try {
Image::make($path)->save(public_path('storage/posts/' . $filename));
} catch (\Intervention\Image\Exception\NotReadableException $e) {
Image::make($path)->save(public_path('storage/posts/' . 'https://lh3.googleusercontent.com/yI8LPDBhjvqLR1mQMitJlibZdWqaYAlMVUJK6zpBQkOb_Bk03qn_l2SQyn5yY__KZcY-=w300-rw'));
}
$post = new Post;
$post->title = $title;
$post->body = '..';
$post->excerpt = $description;
$post->meta_description = $link;
$post->image = 'posts/' . $filename;
$post->save();
}
它所做的是从 JSON URL 中获取内容并将其存储到基于 LaraveL 的数据库中。所以问题是它存储从最新到最旧的数据,但我想要的是从最旧到最新存储项目。这是第一个问题,第二个问题是如何让它只存储最新的而不复制那些已经存储的并且只检查那些是新的?