Pull to refresh
0
0
Алексей @sunswordred

User

Send message

Определение города и региона России по IP

Reading time 2 min
Views 7.8K
И так, 13 Марта, 2009 16:07 появилась следующая новость на данном ресурсе "Долгожданный XML-сервис!!!".

Это событие необычайно меня обрадовало, ещё бы такой сервис на таком ресурсе и сразу ручки шаловливые зачесались!

Буквально через 10 минут был готов простейший код запроса ip, в ходе обсуждений он получился следующим (сделано на curl):

  function getip($GetRIP)
  {
    $data = "<ipquery><fields><all/></fields><ip-list><ip>".$GetRIP."</ip></ip-list></ipquery>";
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, "http://194.85.91.253:8090/geo/geo.html");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    $xml = curl_exec($ch);
    curl_close($ch);
    
    //echo $xml;
    
    $messge="!<message>(.*?)</message>!si";
    
    preg_match($messge, $xml, $main_ar["message"]);
    
    if($main_ar["message"][1]!="Not found")
    {
      $district="!<district>(.*?)</district>!si";
      $region="!<region>(.*?)</region>!si";
      $town="!<city>(.*?)</city>!si";
      
      preg_match($district, $xml, $main_ar["district"]);
      preg_match($region, $xml, $main_ar["region"]);
      preg_match($town, $xml, $main_ar["city"]);
      
      $ArMain=array("FIND"=>1,"DISTRICT"=>$main_ar["district"][1], "REGION"=>$main_ar["region"][1],"TOWN"=>$main_ar["city"][1]);
      return $ArMain;    
    }
    else return array("FIND"=>0);    
  }


* This source code was highlighted with Source Code Highlighter.


Читать дальше →
Total votes 12: ↑11 and ↓1 +10
Comments 13

Information

Rating
Does not participate
Registered
Activity