Getting the JSON reponse from an API with Laravel

24,417

Solution 1

You can use file_get_contents()

Route::get('/directions', function() {
    $origin = Input::get('origin');
    $destination = Input::get('destination');

    $url = urlencode ("http://maps.googleapis.com/maps/api/directions/json?origin=" . $origin . "&destination=" . $destination . "&sensor=false");

    $json = json_decode(file_get_contents($url), true);

    dd($json);
});

Solution 2

you can try this step...

set route...

Route::get('/json', [yourController::class, 'getJSON']);

and the method for get value as json format

public function getJSON(Request $request)
{

    $url = 'https://api******';

    $response = file_get_contents($url);
    $newsData = json_decode($response);


    return response()->json($newsData);         

}

Solution 3

Take a look at my package

https://github.com/joshhornby/Http

Hopefully makes it a little easier to call APIs

Share:
24,417
dabadaba
Author by

dabadaba

Updated on February 09, 2021

Comments

  • dabadaba
    dabadaba about 3 years

    I am just messing with APIs and now I'm trying to use Google Directions API in my app.

    I made a form to get the user's input and retrieve this data and create the URI just in the routes.php file:

    Route::get('/directions', function() {
        $origin = Input::get('origin');
        $destination = Input::get('destination');
    
        $url = "http://maps.googleapis.com/maps/api/directions/json?origin=" . $origin . "&destination=" . $destination . "&sensor=false";
    });
    

    That URL's response is a JSON. But now, how am I supposed to store that response with Laravel? I have been looking at the Http namespace in Laravel and I didn't find a way. If it can't be done with Laravel, how can it be done with plain php?

  • dabadaba
    dabadaba almost 10 years
    thanks, that gets closer to what I wanted. However, 1: doesn't laravel provide any way to do this? and 2, I'm getting a 400 Bad Request with some characters. I tried wrapping the result of file_get_contents with utf8_encode but it doesn't do the trick. Any ideas?
  • dabadaba
    dabadaba almost 10 years
    also dd is not "pretty printing" for some reason
  • Laurence
    Laurence almost 10 years
    dd is just to dump the variable to show you it works. You can do whatever you want with the variable to print it how you want.
  • Laurence
    Laurence almost 10 years
    As for "doesn't laravel provide any way to do this" - this is the way you do it. The other option is to use a package like Guzzle and curl the request - github.com/guzzle/guzzle
  • Laurence
    Laurence almost 10 years
    Finally - I have modified my answer to include urlencode() to make it work for the other chars
  • dabadaba
    dabadaba almost 10 years
    I tried urlencode() too but it does not work: file_get_contents(http%3A%2F%2Fmaps.googleapis.com%2Fmaps%2F‌​api%2Fdirections%2Fj‌​son%3Forigin%3Dlos+a‌​ngeles%26destination‌​%3Dnew+york%26sensor‌​%3Dfalse): failed to open stream: No such file or directory The only thing that works is replacing white spaces with %20 but this seems like an odd solution (I have encountered this in the past with a webapp made with python).
  • dabadaba
    dabadaba almost 10 years
    I'll give it a look, looks good. I hope Laravel implements such thing.
  • Jared Smith
    Jared Smith about 3 years
    "Take a look at my package" did you do that on purpose or was it inadvertent?
  • Pooya Estakhri
    Pooya Estakhri over 2 years
    @JaredSmith pointing out the response->json() justifies it, also this is first result when searching for laravel json api so question’s age does not matter