Ruby on Rails için PHP kodu dönüştürmek

1 Cevap php

Ben raylar uygulama üzerinde bir yakut için geoip web hizmetini kullanmak çalışıyorum. Onlar herhangi bir yakut demolar vermek istemiyorum ama bu onlar için PHP vermek budur. Herkes raylar üzerinde yakut çalışmak için bu dönüştürmek için nasıl biliyordu merak ediyordum? Ben sadece veri kenti ve bölgeyi gerekir. Daha fazla örnek kendi sitesinde bulunabilir

$query = "http://geoip3.maxmind.com/b?l=" . $license_key . "&i=" . $ipaddress;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout)
or die('Can not open connection to server.');
if ($fp) {
  fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
  while (!feof($fp)) {
    $buf .= fgets($fp, 128);
  }
  $lines = split("\n", $buf);
  $data = $lines[count($lines)-1];
  fclose($fp);
} else {
  # enter error handing code here
}

echo $data;
$geo = explode(",",$data);
$country = $geo[0];
$state = $geo[1];
$city = $geo[2];
$lat = $geo[3];
$lon = $geo[4];

1 Cevap

Ben bu yüzden tam olarak test edilemez bir geoip lisans anahtarı yok, ama bu çalışması gerekir:

require 'net/http'

url = 'http://geoip3.maxmind.com/b?l=%s&i=%s' % [license_key, ip_address]
res = Net::HTTP.get_response(URI.parse(url))

lines = res.body.split("\n")

geo = lines[-1].split(',')

country = geo[0]
state = geo[1]
city = geo[2]
lat = geo[3]
lon = geo[4]