我想将第三方 api 的数据存储到我的数据库中,听起来很基本,但我有点困惑。
我的 Controller 中有这个
public function showTips()
{
$client = new Client(['headers' => ['Accept' => 'application/json']]);
$res = $client->request('GET', 'https://example.com/api/xxx/xxxapiKey=xxxx');
$data = json_decode($res->getBody()->getContents());
$events = $data->Data;
dd($events);
//return view('tips')->with(compact('events'));
}
我得到的数据:
array:2 [▼
0 => {#8824 ▼
+"timeLive": "76"
+"matchID": "10482C5"
为了接收“timeLive”数据并将其存储在 MySQL 表中,我需要进行哪些更改? 我们将不胜感激每一个帮助!
最佳答案
你应该像这样循环 foreach 结果:
$data = json_decode($res->getBody()->getContents(),true);
$events = $data['Data'];
foreach($events as $item)
{
DB::table('your_table')->insert(['timeLive'=>$item['timeLive']])
}
witch timeLive 是表“your_table”中的列
关于mysql - Laravel 5.8 将 API 数据保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57927836/