1

I am trying to use ip-api.com (IP Geolocation) to retrieve usage limit.

1st - I've managed to use the api with php, but they don't give the usage limit (max 45/minute) in the response format. They say that that information is returned in the HTTP header.

2nd - So I opened chrome developer network to see if I find the variables (X-R1 and X-Ttl) or whatever they called in the HTTP header but I don't see them there.

3rd - What I've managed to do is using php function (get_headers($url)), it works fine but when ever the script is run it counts as 2 requests for the API, so the usage limits always counts as 2.

How do I find them in the 2nd step or with some php function so I don't have to make another request like i did in the 3rd?

<?php
//1st request
function getClientIp(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        //ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        //ip pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$clientIp = getClientIp();
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$clientIp));
if ($query['status'] == 'success'){
    echo $query['city'];
    echo '<br>';
    echo $query['query'];
};

//2nd Request
echo '<br><br>';
$url = 'http://ip-api.com/php/'.$clientIp;
//print_r(get_headers($url));
print_r(get_headers($url, 1)['X-Rl']);
skillsboy
  • 319
  • 6
  • 14
  • 1
    I'd say instead of `file_get_contents()`, use cURL. See [this question](https://stackoverflow.com/questions/9183178/can-php-curl-retrieve-response-headers-and-body-in-a-single-request) and the accepted answer for how you can make a single request and get the response body _and_ headers. – Patrick Q Dec 11 '19 at 14:47
  • Thank you Patrick Q, I found what I wanted when I when through the post you linked. I was looking for cURL solution but as I started with file_get_contents() I read a little more and in the same post I found the array $http_response_header can dump all of its header information I needed. Thank you! – skillsboy Dec 11 '19 at 15:45
  • Glad you got it working – Patrick Q Dec 11 '19 at 15:54

1 Answers1

0

My solution in the end was just using the array variable $http_response_header.

Like this I can get the API response + header in a single request.

<?php
function getClientIp(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        //ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        //ip pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

function parseHeaders( $headers )
{
    $head = array();
    foreach( $headers as $k=>$v )
    {
        $t = explode( ':', $v, 2 );
        if( isset( $t[1] ) )
            $head[ trim($t[0]) ] = trim( $t[1] );
        else
        {
            $head[] = $v;
            if( preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#",$v, $out ) )
                $head['reponse_code'] = intval($out[1]);
        }
    }
    return $head['X-Rl'];
}

//body request
$clientIp = getClientIp();
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$clientIp));
if ($query['status'] == 'success'){
    echo $query['city'];
    echo '<br>';
    echo $query['query'];
}

//header request
echo '<br>Available requests: '.parseHeaders($http_response_header);    
skillsboy
  • 319
  • 6
  • 14