vendredi 6 janvier 2017

Laravel: I keep getting localhost's ip

I'm assigning ip manually through this line :

$data = json_decode(file_get_contents("http://ipinfo.io/".$ip."/geo"));

In here

getClientInformation.php

namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Session; 
use Illuminate\Support\Facades\Request;
class getClientInformation
{
    public function handle($request, Closure $next)
    {
        if(!Session::get("clientInfo"))
        {

            $ip   = "109.228.19.126";

            $data = json_decode(file_get_contents("http://ipinfo.io/".$ip."/geo"));    
            Session::set("clientInfo", $data);   
            $request->attributes->add(["clientInfo" => $data]);

        }

        $request->attributes->add(["clientInfo" => Session::get("clientInfo")]);

        return $next($request);  
    }}

Now I in the route

web.php (Laravel 5.3)

Route::get("/info", ['middleware' => "getClientInfo",
        function()
        {
            $clientInfo = Request()->attributes->get("clientInfo");

            print_r($clientInfo);

        }]);

It gives me

stdClass Object ( [ip] => ::1 [bogon] => 1 )

If I hit manually in browser via http://ift.tt/2iLat1b, The data should be:

{
  "ip": "109.228.19.126",
  "city": "Gloucester",
  "region": "England",
  "country": "GB",
  "loc": "51.8333,-2.2500",
  "postal": "GL1"
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hZyLG5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire