为了账号安全,请及时绑定邮箱和手机立即绑定

从 Php Laravel 中的 Api 响应填充数据库

从 Php Laravel 中的 Api 响应填充数据库

PHP
慕田峪9158850 2023-09-22 16:10:28
拜托,我在尝试从 Guzzlehttp 访问此 Api 响应时一直头疼,格式是这里的问题,我不明白我需要帮助。大家好!拜托,我在尝试从 Guzzlehttp 访问此 Api 响应时一直头疼,格式是这里的问题,我不明白我需要帮助。这是代码namespace App\Http\Controllers;use Illuminate\Http\Request;use GuzzleHttp\Client;use App\Country;use App\Globas;class DataController extends Controller{public function getClient(){ $client=new Client();  $response = $client->request('GET', 'https://api.covid19api.com/summary');  $data= json_decode($response->getBody()->getContents(),true);//delete all row in the database tableGlobas::truncate();Country::truncate();  foreach($data as $da){   Globas::create($da);   Country::create([       'Country'=> $data['Country'],       'CountryCode'=>$data['CountryCode'],       'Slug'=>$data['Slug'],       'Date'=>$data['Date'],       'NewConfirmed'=>$data['NewConfirmed'],        'TotalConfirmed'=>$data['TotalConfirmed'],           'NewRecovered'=>$data['NewRecovered'],              'TotalRecovered'=>$data['TotalRecovered'],               'NewDeaths'=>$data['NewDeaths'],               'TotalDeaths'=>$data['TotalDeaths']   ]);return $data;  }}}**This is the Response** {"Global":{"NewConfirmed":259470,"TotalConfirmed":19636772,"NewDeaths":5457,"TotalDeaths":726765,"NewRecovered":201182,"TotalRecovered":11938458},"Countries":[{"Country":"Afghanistan","CountryCode":"AF","Slug":"afghanistan","NewConfirmed":39,"TotalConfirmed":37054,"NewDeaths":5,"TotalDeaths":1312,"NewRecovered":57,"TotalRecovered":25960,"Date":"2020-08-09T17:38:36Z","Premium":[]},......{"Country":"Zimbabwe","CountryCode":"ZW","Slug":"zimbabwe","NewConfirmed":124,"TotalConfirmed":4575,"NewDeaths":0,"TotalDeaths":102,"NewRecovered":71,"Total``Recovered":1416,"Date":"2020-08-09T17:38:36Z","Premium":[]}],"Date":"2020-08-09T17:38:36Z"}**Country Model**<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Country extends Model{   protected $fillable=['Country','CountryCode','Slug','Date',   'NewConfirmed', 'TotalConfirmed','NewRecovered','TotalRecovered','NewDeaths','TotalDeaths'];}谢谢...
查看完整描述

3 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

我收到 ErrorException (E_NOTICE) 未定义索引:NewConfirmed


当我做了这样的事情时


 foreach($data['Countries'] as $country){



     $counter=   Country::create([

               'Country'=> $country['Country'],

               'CountryCode'=>$country['CountryCode'],

               'Slug'=>$country['Slug'],

               'Date'=>$country['Date'],

               'NewConfirmed'=>$country['NewConfirmed'],

                'TotalConfirmed'=>$country['TotalConfirmed'],

                   'NewRecovered'=>$country['NewRecovered'],

                      'TotalRecovered'=>$country['TotalRecovered'],

                       'NewDeaths'=>$country['NewDeaths'],

                       'TotalDeaths'=>$country['TotalDeaths']

           ]);


            }



   foreach($data as $da){

    $globals=Globas::create([

        'NewConfirmed'=>$da['NewConfirmed'],

        'TotalConfirmed'=>$da['TotalConfirmed'],

                       'NewRecovered'=>$da['NewRecovered'],

                          'TotalRecovered'=>$da['TotalRecovered'],

                           'NewDeaths'=>$da['NewDeaths'],

                           'TotalDeaths'=>$da['TotalDeaths']

    ]);

    

    }

}


查看完整回答
反对 回复 2023-09-22
?
月关宝盒

TA贡献1772条经验 获得超5个赞

在 foreach 内部,您应该引用 $da 变量,而不是该位置的 $data:


Country::create([

       'Country'=> $data['Country'],

       'CountryCode'=>$data['CountryCode'],

       'Slug'=>$data['Slug'],

       'Date'=>$data['Date'],

       'NewConfirmed'=>$data['NewConfirmed'],

        'TotalConfirmed'=>$data['TotalConfirmed'],

           'NewRecovered'=>$data['NewRecovered'],

              'TotalRecovered'=>$data['TotalRecovered'],

               'NewDeaths'=>$data['NewDeaths'],

               'TotalDeaths'=>$data['TotalDeaths']

   ]);

另一点是国家也是一个数组,因此您可能应该为国家创建另一个 foreach。

https://img1.sycdn.imooc.com//650d4c3600016f2305420668.jpg

foreach ($data['Countries'] as $country)
{

}


查看完整回答
反对 回复 2023-09-22
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

您必须为Global::create()and的返回值分配一个变量Country::create()。像这样$global = Global::create(...)运行$global->save()将其存储在数据库中。


编辑:根据您最新的回复,您不应该迭代数据,因为返回的数据不是数组。对于全局变量,您不需要迭代。你可以这样做:


$response = $client->request('GET', 'https://api.covid19api.com/summary');

$data = json_decode($response->getBody()->getContents(),true);


$globals=Globas::create([

    'NewConfirmed'=>$data['NewConfirmed'],

    'TotalConfirmed'=>$data['TotalConfirmed'],

    'NewRecovered'=>$data['NewRecovered'],

    'TotalRecovered'=>$data['TotalRecovered'],

    'NewDeaths'=>$data['NewDeaths'],

    'TotalDeaths'=>$data['TotalDeaths']

]);


$globals->save();


foreach($data['Countries'] as $country) {

     $country = Country::create([

               'Country'=> $country['Country'],

               'CountryCode'=>$country['CountryCode'],

               'Slug'=>$country['Slug'],

               'Date'=>$country['Date'],

               'NewConfirmed'=>$country['NewConfirmed'],

               'TotalConfirmed'=>$country['TotalConfirmed'],

               'NewRecovered'=>$country['NewRecovered'],

               'TotalRecovered'=>$country['TotalRecovered'],

               'NewDeaths'=>$country['NewDeaths'],

               'TotalDeaths'=>$country['TotalDeaths']

           ]);


    $country->save();

}


查看完整回答
反对 回复 2023-09-22
  • 3 回答
  • 0 关注
  • 145 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信