It is necessary on the server side (in PHP) to set the country (and city) of the user, so that depending on this to display this or that content.

Found a resource ( http://ipgeobase.ru:7020/geo?ip= ) and judging by the description of a good, updated. Nowhere did I find how to work with it correctly and whether it can be applied in my case. All I need is to substitute the IP address of the incoming User into this URL and then retrieve the data that is in this resource as XML.

I have prescribed such a script:

function get_city_by_ip($ip) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://ipgeobase.ru:7020/geo?ip='.$ip); curl_setopt($ch, CURLOPT_HEADER, false); $data = curl_exec($ch); $city = ( !curl_errno($ch) && $xml = simplexml_load_string($data) ) ? $xml->ip->city : false; curl_close($ch); return $city; } var_dump( get_city_by_ip( $_SERVER['REMOTE_ADDR'] ) ); 

But I don’t get any content from this resource. I get

 bool(false) 

What am I doing wrong or forgot to register? Perhaps there are other ways to tutor the user's country, but I need such external sources where I could send the curl and get the data, install an additional database and the libraries in my case are not an option.

  • I can tell you that the country can be defined in your web server - google by nginx, geoip. Accuracy is not a fact that there will be space, but it works almost out of the box - gecube
  • Regarding debugging, you can drive the curl from the console in order to understand what the problem is in general - in the site's API, in a chicken or in php. And then integrate the code. Well, and usually third-party services require a token (authorization, in short). There are really no free services yet .... - gecube

1 answer 1

Here is a working example of the function for this service.

 function get_city_by_ip($ip) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://ipgeobase.ru:7020/geo?ip=' . $ip); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HEADER, 0); $data = curl_exec($ch); $ip_answer = simplexml_load_string($data); $city = ($ip_answer -> ip[0] -> city); curl_close($ch); return $city; } 

It would be more convenient if he gave out json. enter image description here

  • And does this code work for you at the moment? I replaced my function with yours, then I registered to see the result: $ city = get_city_by_ip ($ _SERVER ['REMOTE_ADDR']); var_dump ($ city); and it gives me NULL. What could be the problem? - Littus
  • What are you using as a Http server? Perhaps your server incorrectly gives REMOTE ADDR. try echo $ _SERVER ['REMOTE_ADDR']; or something like that. - Andrey
  • I have the function code that I specified, plus the encoding displayed that screenshot in response - Andrey
  • that's just $ _SERVER ['REMOTE_ADDR']; gives the correct IP address, - with this all the rules. Since I then insert this IP address into the resource ipgeobase.ru:7020/geo?ip=, and this service perfectly correctly and specifically indicates all data to me. The problem is something else. Maybe the server itself on which the site is located has some additional limitations. I do not know. - Littus
  • Maybe the script does not have access to the Internet ... Try $data = curl_exec($ch); $ data variable after running $data = curl_exec($ch); - Andrey