Laravel ErrorException 使用 file_get_contents()

Posted

技术标签:

【中文标题】Laravel ErrorException 使用 file_get_contents()【英文标题】:Laravel ErrorException using file_get_contents() 【发布时间】:2019-11-23 02:05:00 【问题描述】:

大家好,我是 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】:

看看这个堆栈溢出问题的答案的 cmets:https://***.com/a/9802864/141708

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

鉴于您使用的是 laravel,您也可以考虑在 Laravel 中使用一个包,再次查看此堆栈溢出问题以了解如何采用这种方式的选项:Doing HTTP requests FROM Laravel to an external API

【讨论】:

以上是关于Laravel ErrorException 使用 file_get_contents()的主要内容,如果未能解决你的问题,请参考以下文章

Laravel ErrorException 使用 file_get_contents()

带有消息“未定义变量”的 Laravel Tinker ErrorException

php artisan serve [ErrorException] laravel

laravel - ErrorException 数组到字符串的转换

StreamBuffer.php 第 95 行中的 ErrorException:在 laravel 5 中

Laravel 6 ErrorException 未定义变量:用户(...home.blade.php)