Laravel ErrorException使用file_get_contents()

时间:2019-07-13 17:59:39

标签: laravel-5

大家好,我是laravel的新手,我遇到了这个错误。我真的不知道这是什么,所以我希望有人可以帮助我解决问题!

E/errror: {
    "message": "file_get_contents(https://maps.googleapis.com/maps/api/directions/json?origin=**.89**9029,**.8584**5&destination=**.897**1522708,**.858512**80171&mode=driving&key=**********): failed to open stream: Connection timed out",
    "exception": "ErrorException",
    "file": "/home/**/domains/**.tech/app/Http/Controllers/Helper/GoogleController.php",
    "line": 99,
    "trace": [
        {
            "function": "handleError",
            "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
            "type": "->"
        },
        {
            "file": "/home/**/domains/**.tech/app/Http/Controllers/Helper/GoogleController.php",
            "line": 99,
            "function": "file_get_contents"
        },
        {
            "file": "/home/**/domains/**.tech/app/Http/Controllers/Api/BookingController.php",
            "line": 1854,
            "function": "GoogleDistanceAndTime",
            "class": "App\\Http\\Controllers\\Helper\\GoogleController",
            "type": "::"
        },
        {
            "file": "/home/**/domains/**.tech/app/Http/Controllers/Api/BookingController.php",
            "line": 1831,
            "function": "EtaCalculation",
            "class": "App\\Http\\Controllers\\Api\\BookingController",
            "type": "->"
        },
        {
            "function": "Tracking",
            "class": "App\\Http\\Controllers\\Api\\BookingController",
            "type": "->"
        },
        {
            "file": "/home/**/domains/**.tech/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
            "line": 54,
            "function": "call_user_func_array"
        },
        {
            "file": "/home/**/domains/**.tech/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
            "line": 45,
            "function": "callAction",
            "class": "Illuminate\\Routing\\Controller",
            "type": "->"
        },
        {
            "file": "/home/**/domains/**.tech/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line": 219,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\ControllerDispatcher",
            "type": "->"
        },
        {
            "file": "/home/**/domains/**.tech/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line": 176,
            "function": "runController",
            "class": "Illuminate\\Routing\\Route",
            "type": "->"
        },
        {
            "file": "/home/**/domains/**.tech/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 680,
            "function": "run",
            "class": "Illuminate\\Routing\\Route",
            "type": "->"
        },
        {
            "file": "/home/**/domains/**.tech/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 30,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/**/domains/**.tech/app/Http/Middleware/ValidUser.php",
            "line": 25,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/**/domains/**.tech/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 163,
            "function": "handle",
            "class": "App\\Http\\Middleware\\ValidUser",
            "type": "->"
        },
        {
            "file": "/home/**/domains/**.tech/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type
responseFromServerError

这是laravel内部的功能:

  

App / Http / Controllers / Helper / GoogleController.php

我真的迷路了,不知道发生了什么

public static function GoogleDistanceAndTime($from, $to, $key)
{
    $data = file_get_contents("https://maps.googleapis.com/maps/api/directions/json?origin=$from&destination=$to&mode=driving&key=$key");
    $data = json_decode($data, true);
    $status = $data['status'];
    if ($status != "OK") {
        return array('time' => "", 'distance' => "");
    } else {
        $time = $data['routes'][0]['legs'][0]['duration']['text'];
        $distance = $data['routes'][0]['legs'][0]['distance']['text'];
        return array('time' => $time, 'distance' => $distance);
    }
}

请我显示您需要的任何东西。谢谢!

1 个答案:

答案 0 :(得分:2)

请查看有关此堆栈溢出问题答案的评论:https://stackoverflow.com/a/9802864/141708

很可能在您的php安装中禁用了fopen包装器,您应该使用curl_exec来调用该服务。有关在php中使用curl功能的信息,请参见:https://php.net/manual/en/book.curl.php

鉴于您使用的是幼虫,您也可以考虑在Laraval中使用一个软件包,再次,请检查此堆栈溢出问题以获取执行此操作的选项:Doing HTTP requests FROM Laravel to an external API