Ivijan Stefan Stipić
Ivijan Stefan Stipić

Reputation: 6668

PHP: cURL refuse connection to URL and send GET information on some server

I made function to read GeoPlugin data for my websites and on one server I find wierd issue. All cURL request are refused. Here is part of my code:

protected $url='http://www.geoplugin.net/json.gp?ip={IP}&base_currency={CURRENCY}';

protected function __get_data($ip=false, $currency='')
    {
        // Current or custom IP
        $ip = ((is_bool($ip) && $ip==false) ? $this->__ip() : $ip);
        if($ip!='127.0.0.1' || $ip!='0.0.0.0')
        {
            // Configure GET function
            $url = str_replace('{IP}', $ip, $this->url );
            if(empty($currency))
                $url = str_replace( '&base_currency={CURRENCY}', '', $url);
            else
                $url = str_replace( '{CURRENCY}', $currency, $url);
            // Get content from URL
            if(function_exists("curl_init"))
            {
                $cURL = curl_init();
                curl_setopt($cURL, CURLOPT_URL, $url);
                curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT ,5); 
                curl_setopt($cURL, CURLOPT_TIMEOUT , 2);
                curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Accept: application/json'));
                $result = curl_exec($cURL);
                curl_close($cURL);
            }
            else
            {
                $result = file_get_contents($url);
            }
            // Return objects from JSON data
            if($result!=false)
            {
                return json_decode($result);
            }
            else return false;
        }
        else return false;
    }

    ## find real IP adress of visitor ##
    protected function __ip()
    {
        $findIP=array(
            'HTTP_CLIENT_IP',
            'HTTP_X_FORWARDED_FOR',
            'HTTP_X_FORWARDED',
            'HTTP_X_CLUSTER_CLIENT_IP',
            'HTTP_FORWARDED_FOR',
            'HTTP_FORWARDED',
            'REMOTE_ADDR'
        );
        $ip = '';
        foreach($findIP as $http)
        {
            if(function_exists("getenv"))
            {
                $ip = getenv($http);
            }
            else
            {
                if (array_key_exists($http, $_SERVER) !== false){
                    foreach (explode(',', $_SERVER[$http]) as $findIP){
                        $ip = trim($findIP);

                    }
                }
            }
            if(function_exists("filter_var") && !empty($ip))
            {
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) return $ip;

            }
            else if(preg_match('/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $ip) && !empty($ip))
            {
                return $ip;
            }
        }
        return '0.0.0.0';
    }

On arround90 websites everithing work perfectly, on one website with var_dump() I find that connection are refused. I try also with file_get_contents and the same results. I try also just cURL call in some test PHP file separated from website and the same results. What can be a problem?

Upvotes: 0

Views: 1302

Answers (2)

Michael C.
Michael C.

Reputation: 1457

cURL might be disabled in your server.

Please run the phpinfo() to check the status of cURL.

If it is disabled, please install cURL and enable it in PHP.

Upvotes: 0

Vika Marquez
Vika Marquez

Reputation: 353

  1. It's may be DNS problem;

  2. It's may be poor connection (more time needed for loading);

  3. Your query may be banned from target server, because from your IP (source's server IP) too much queries for a time, more than limits.

What you can do:

  1. Make sure that you can open target url from source server without using cURL (if you use simple hosting, I mean not VPS, you'll can't check it);

  2. Increase values for CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT;

  3. If problem will not solved, you should use proxy with cURL (look for official documentation about CURLOPT_PROXY and other proxy options for curl_setopt function).

Upvotes: 1

Related Questions