Limman mysは、Zininde API Kullan


パブリックAPI Kullanの


Bu - yaz - se - daパブリックBir api ' nin liman - eklentisinde NAS
Kodlara Linkten Ultra - Ariirsinizhttps://github.com/abugraokkali/Liman-Covid-Stats

  • アプリケーション/コントローラ/apicontroller.PHP
  •   use GuzzleHttp\Client;
    
    Le Olaak Dosyamは、Zda guzzleHTTP\クライアントKullanacaのルースをM . ZのZを示します.
    GUZIL、HTTP iStekerini Gは、ndermeyi veウェブservisleriyle entegre olmayはkolaylaのtのtをリリースしました.つぼBir bilgiye gist yazは、mdan - ulaを得ます.
    https://gist.github.com/abugraokkali/8d99177a7af4daa62b2d63e3941fdd88
      public function getResponse()
      {
        $client = new GuzzleHttp\Client(
            ['base_uri' => 'https://api.covid19api.com/']
        );
        $response = $client->request('GET', 'summary');
        return json_decode($response->getBody()->getContents());
    
      }
    
    ベースURI ' yは、https://api.covid19api.com/ Olan Bir guzzleHTTP\クライアントobjesi oluを得ます.Request ' Lerimizi Bu Clientは、Zapinden Yapaca Duragon Zに依頼します.
    クライアントNesnesinin要求Methoduyla/概要enpoint ' ineは、sorgusuヤップを得ます.JSON形式はNDAKI応答をデコードします.
    リクエスト:https://api.covid19api.com/summary
    レスポンス
      {
        "ID": "17cf60ab-05a9-4a5d-8027-458354300853",
        "Message": "",
        "Global": {
          "NewConfirmed": 306062,
          "TotalConfirmed": 205102230,
          "NewDeaths": 3858,
          "TotalDeaths": 4331537,
          "NewRecovered": 0,
          "TotalRecovered": 0,
          "Date": "2021-08-13T07:08:37.28Z"
        },
        "Countries": [
          ...
          {
            "ID": "b1cecf17-faac-4e60-b5e3-9266759234a6",
            "Country": "Turkey",
            "CountryCode": "TR",
            "Slug": "turkey",
            "NewConfirmed": 0,
            "TotalConfirmed": 6018455,
            "NewDeaths": 0,
            "TotalDeaths": 52703,
            "NewRecovered": 0,
            "TotalRecovered": 0,
            "Date": "2021-08-13T07:08:37.28Z",
            "Premium": {}
          },
          ...
         ],
        "Date": "2021-08-13T07:08:37.28Z"
       }
    
      public function listCountries()
      {
          $response = $this->getResponse();
          $countries = (array) $response->{'Countries'};
          $data = [];
        foreach($countries as $country){
            $data[] = [
                "Country" => $country->{'Country'},
                "CountryCode" => $country->{'CountryCode'},
                "TotalConfirmed" => $country->{'TotalConfirmed'},
                "TotalDeaths" => $country->{'TotalDeaths'},
            ];
        }
        return view('table', [
            "value" => $data,
            "title" => ["Ülke","Ülke Kodu","Vaka Sayısı","Vefat Sayısı"],
            "display" => ["Country","CountryCode","TotalConfirmed","TotalDeaths"]
        ]);
    
      }
    
    bu FonkSiION getResponse () ' unは、rをrにします.
    シュテッツェル;
  • getResponse ()は、引き数r r { l }を返します.
  • ' getResponse (' unetETTI )を返します.
  • Large - ile - ge - origin - ilyor - ve - iSenen Attributes ' lar Data Adult Nda BIR Array ' de Tutuluyor ,
  • に対して、配列を指定します.
  • データArray ' i View ' Parametre Olarak Verilip
  • ediliyorを返します.

  • 路線PHP
  • <?php
    return [
      ...
      "list_countries" => "ApiController@listCountries",
      ...
    ];
    

  • ビュー/国/メイン.ブレード
  • <div class="row">
        <div class="col-12 mb-2">
            <div class="table-responsive" id="countriesTable"></div>
        </div>
    </div>
    
    @include("countries.scripts")
    
    タブローIは、Bir div oluにおいて、Turuyoruzに属します.

  • ビュー/国/スクリプト.ブレード
  • <script>
        function listCountries(){
            showSwal("{{__('Yükleniyor...')}}", 'info');
            let data = new FormData();
            request("{{API('list_countries')}}", data, function(response){
                $('#countriesTable').html(response).find('table').DataTable(
                                                                  dataTablePresets('normal');
                Swal.close();
            }, function(response){
                response = JSON.parse(response);
                showSwal(response.message, 'error');
            });
        }
    </script>
    
    Edilen View ' i olu ' s turdu ' u umuz div ' e koyuyoruz ve tabloyu bast - nt - rm - origin - oluyoruz .