[英]storing json_decode data from oldest to newest in MySql database
我有以下代码:
$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的数据库中。 所以问题是它从最新到最旧存储数据,但我要存储的项目是从最旧到最新。 那是第一个问题,第二个问题是如何使它仅存储最新的而不重复已存储的,而仅检查仅新的?
这是通过使用array_reverse来完成的:
$url = 'http://example.com/523223.json?';
$json= file_get_contents($url);
$data = json_decode($json);
$rows = $data->{'items'};
foreach (array_reverse($rows) as $row) {
echo '<p>';
$title = $row->name;
$description = $row->description;
$link = $row->link;
$image_link = $row->thumbnail;
$path = $image_link;
$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->slug = $link;
$post->image = 'posts/' . $filename;
$post->save();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.