0

Hello guys i'm new to laravel and I have this error. I don't really know what this is at all so i hope someone could help me and solve it!

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

And this is the function inside laravel:

App / Http / Controllers /Helper / GoogleController.php

I'm really lost don't know what happened

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);
    }
}

Ask me to show anything you need. Thanks!

1 Answer 1

2

Have a look at the comments on the answer to this stack overflow question: https://stackoverflow.com/a/9802864/141708

It is likely that fopen wrappers are disabled on your php installation and you should instead use curl_exec to call the service. Info on using the curl capabilities in php are here: https://php.net/manual/en/book.curl.php

Given you're using laravel you could also consider using a package within Laravel, again, check out this stack overflow question for options to do it that way: Doing HTTP requests FROM Laravel to an external API

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.