Advertisement
Guest User

Untitled

a guest
Nov 8th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Created by PhpStorm.
  5.  * User: Julio Costa
  6.  * Date: 08/11/2019
  7.  * Time: 22:31
  8.  */
  9.  
  10. namespace App\Services;
  11.  
  12. use GuzzleHttp\Client;
  13. use GuzzleHttp\Exception\GuzzleException;
  14. use Illuminate\Support\Arr;
  15.  
  16. class NasaService
  17. {
  18.  
  19.  
  20.     public function getMeteor($start_date, $end_date)
  21.     {
  22.  
  23.         $arr = ['diamter' => [], 'meteor_orbiting' => []];
  24.         $client = new Client();
  25.         $params = [
  26.             'start_date' => $start_date,
  27.             'end_date' => $end_date,
  28.             'api_key' => config('nasa')['key']
  29.         ];
  30.         try {
  31.             $response = $client->request(
  32.                 'GET',
  33.                 config('nasa')['url'] . '/v1/feed',
  34.                 [
  35.                     'query' => $params
  36.                 ]
  37.             );
  38.             $contentType = $response->getHeader('Content-Type');
  39.             if ($contentType && strpos($contentType[0], 'application/json') !== false) {
  40.  
  41.                 $result = json_decode($response->getBody())->near_earth_objects;
  42.  
  43.                 foreach ($result as $meteor) {
  44.  
  45.                     foreach ($meteor as $obj) {
  46.                         array_push(
  47.                             $arr['diamter'],
  48.                             [$obj->estimated_diameter]
  49.                         );
  50.                         $first = Arr::first($obj->close_approach_data, function ($value, $key) {
  51.                             return $value;
  52.                         });
  53.                         array_push(
  54.                             $arr['meteor_orbiting'],
  55.                             [$first]
  56.                         );
  57.                     }
  58.                 }
  59.             }
  60.         } catch (GuzzleException $e) {
  61.             dd($e->getMessage());
  62.         }
  63.  
  64.         return response()->json($arr);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement