Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: Julio Costa
- * Date: 08/11/2019
- * Time: 22:31
- */
- namespace App\Services;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\GuzzleException;
- use Illuminate\Support\Arr;
- class NasaService
- {
- public function getMeteor($start_date, $end_date)
- {
- $arr = ['diamter' => [], 'meteor_orbiting' => []];
- $client = new Client();
- $params = [
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- 'api_key' => config('nasa')['key']
- ];
- try {
- $response = $client->request(
- 'GET',
- config('nasa')['url'] . '/v1/feed',
- [
- 'query' => $params
- ]
- );
- $contentType = $response->getHeader('Content-Type');
- if ($contentType && strpos($contentType[0], 'application/json') !== false) {
- $result = json_decode($response->getBody())->near_earth_objects;
- foreach ($result as $meteor) {
- foreach ($meteor as $obj) {
- array_push(
- $arr['diamter'],
- [$obj->estimated_diameter]
- );
- $first = Arr::first($obj->close_approach_data, function ($value, $key) {
- return $value;
- });
- array_push(
- $arr['meteor_orbiting'],
- [$first]
- );
- }
- }
- }
- } catch (GuzzleException $e) {
- dd($e->getMessage());
- }
- return response()->json($arr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement