SHARE
TWEET

Untitled

a guest Nov 8th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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()
  21.     {
  22.  
  23.         $arr = [];
  24.         $client = new Client();
  25.         $params = [
  26.             'start_date' => '2018-09-07',
  27.             'end_date' => '2018-09-08',
  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.                 foreach ($result as $meteor) {
  43.                     foreach ($meteor as $obj) {
  44.                         $arr['diameter'] = $obj->estimated_diameter;
  45.                         $first = Arr::first($obj->close_approach_data, function ($value, $key) {
  46.                             if ($value->orbiting_body == "Earth") {
  47.                                 return $value;
  48.                             }
  49.                         });
  50.                         $arr['meteor_orbiting'] = $first;
  51.                     }
  52.                 }
  53.             }
  54.         } catch (GuzzleException $e) {
  55.             dd($e->getMessage());
  56.         }
  57.  
  58.         return response()->json($arr);
  59.     }
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top