在 MySql 数据库中按从旧到新的顺序存储 json_decode 数据
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();
}
我有这个代码:
$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();
}