0

In my laravel app I installed stevebauman/location": "1.3.*

It works just fine in my local server. After I uploaded it to the remote server, this line

$contents = json_decode(file_get_contents($this->url.$ip), true);

in the file

F:\xampp\htdocs\news5.2.23\vendor\stevebauman\location\src\Drivers\FreeGeoIp.php

is throwing the error I mentioned.

I searched a lot and tried the following:

  • I echoed the $url and then copied and directly put in browser, it's all right.
  • allow_ini_set("allow_url_include", "on");ini_set("allow_url_fopen", "on"); wrote it in index.php
  • used curl .I will provide the FreeGeoIp Class whereI used curl for testing

    <?php
        namespace Stevebauman\Location\Drivers;
    
        use Stevebauman\Location\Objects\Location;
        use Stevebauman\Location\Location as LocationInstance;
    
        class FreeGeoIp implements DriverInterface
        {
            /**
             * Holds the current Location class instance.
             *
             * @var LocationInstance
            */
            private $instance;
    
            /**
             * Holds the configuration instance.
             *
             * @var \Illuminate\Config\Repository
            */
            private $config;
    
            /*
             * Holds the drivers URL
             */
            private $url;
    
            /**
             * Constructor.
             *
             * @param LocationInstance $instance
            */
            public function __construct(LocationInstance $instance)
            {
                $this->instance = $instance;
    
                $this->config = $this->instance->getConfig();
    
                $this->url = $this->config->get('location.drivers.FreeGeoIp.url');
            }
    
            /**
             * Retrieves the location from the driver FreeGeoIp and returns a    location object.
             *
             * @param string $ip
             *
             * @return Location
            */
             public function get($ip){
    
                 $location = new Location();
                 $file = $this->file_get_contents_curl(urlencode($this->url.$ip));
                 die();       
    
                return $location;
            }
    
            public function file_get_contents_curl($url) {
    
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                var_dump($ch);
                $data = curl_exec($ch);
                if($errno = curl_errno($ch)) {
                    $error_message = curl_strerror($errno);
                    echo "cURL error ({$errno}):\n {$error_message}.</br>";
                }
                curl_close($ch);
                return $data;
            }
        } 
    

Then I found this error

cURL error (6): Couldn't resolve host name.

Finally I got somewhere that maybe the server my site is in has a firewall which is blocking outsourced url call. I dont know why that would be!

Interestingly when I tried return redirect('http://freegeoip.lwan.ws/json/visitor_ip') - this line in a function of one of my Controllers, it worked!

So I have to discard the firewall issue also right?

But when I use file_get_contents('http://freegeoip.lwan.ws/json/visitor_ip') in the same function, it doesn't work again.

People seem to be answering my question without reading it at all. Giving me answers which I already tried and mentioned.Please read.

1 Answers1

0

enable allow_url_fopen from your php.ini file

ssi-anik
  • 2,998
  • 3
  • 23
  • 52