Reputation: 181
I'm trying to use ip-api.com/php but there's a very slow response on my server and I figured out that it is because of the file_get_contents
So basically, I have a pretty simple script (got from github I think)
function get_ip() {
//Just get the headers if we can or else use the SERVER global
if ( function_exists( 'apache_request_headers' ) ) {
$headers = apache_request_headers();
} else {
$headers = $_SERVER;
}
//Get the forwarded IP if it exists
if ( array_key_exists( 'X-Forwarded-For', $headers ) && filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
$the_ip = $headers['X-Forwarded-For'];
} elseif ( array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) && filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 )
) {
$the_ip = $headers['HTTP_X_FORWARDED_FOR'];
} else {
$the_ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );
}
return $the_ip;
}
$ip=get_ip();
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
When it comes to
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
It freezes for around 1min.
Upvotes: 1
Views: 2106
Reputation: 53
In my experience, http://ip-api.com/json didn't respond to the server and it takes much time. When I call from localhost it working fine.
Now I am using https://freegeoip.app/json/ and it's allowed up to 15,000 queries per hour by default.
Upvotes: 0
Reputation: 6689
If you read their API you will see this:
Deprecated Use JSON. Almost all PHP sites now support json_decode(), and it's faster than unserialize()
The reference is here
There you will also find how to do it with Json with an example you can leverage to get your point:
To receive the response in JSON format, send a GET request to
You can supply an IP address or domain to lookup, or none to use your current IP address.
The reference is here
Upvotes: 3