使用Laravel从API获取JSON响应

dab*_*aba 3 php json response laravel laravel-4

我只是在搞乱API,现在我正试图在我的应用程序中使用Google Directions API.

我创建了一个表单来获取用户的输入并检索此数据并在routes.php文件中创建URI :

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";
});
Run Code Online (Sandbox Code Playgroud)

该URL的响应是JSON.但是现在,我怎么能与Laravel一起存储这个回复?我一直在看Laravel中的Http命名空间,但我找不到方法.如果用Laravel无法完成,怎么用普通的php完成?

Lau*_*nce 9

你可以使用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);
});
Run Code Online (Sandbox Code Playgroud)