0

例を挙げて説明しましょう:-

http://www.washingtontimes.com/news/2012/sep/18/pentagon-stops-training-partnering-afghan-troops-b/がユーザーによって送信されたURLであるとしましょう。今、私が必要としているのはphp、javascript、または上記のURLに対応する場所を提供できるその他のウェブスクリプト言語でのメソッド。この場合、結果は「米国」になります。このウェブサイトがホストされている国などです。http://www.site24x7.comのような多くのウェブサイトで行われていますが、それを行うにはコードが必要です

4

2 に答える 2

1

このようにして、URL からホスト名を取得し、gethostbyname() を使用して IP を取得し、whois サイトから IP に関する情報を取得できます。

<?php 
$url  = 'http://www.washingtontimes.com/news/2012/sep/18/pentagon-stops-training-partnering-afghan-troops-b/';

$host = parse_url($url,PHP_URL_HOST);
$ip = gethostbyname($host);
$info = get_ip_info($ip);

$result = array('host'=>$host, 'ip'=>$ip, 'info'=>$info);

print_r($result);
/*
Array
(
    [host] => www.washingtontimes.com
    [ip] => 38.118.71.70
    [info] => Array
        (
            [host] => theconservatives.com
            [country] => United States
            [country_code] => USA
            [continent] => North America
            [region] => Virginia
            [latitude] => 38.9687
            [longitude] => -77.3411
            [organization] => Cogent Communications
            [isp] => Cogent Communications
        )

)
*/
echo $result['info']['country']; //United States

function get_ip_info($ip = NULL){
    if(empty($ip)) $ip = $_SERVER['REMOTE_ADDR'];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'http://www.ipaddresslocation.org/ip-address-locator.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POST,true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,array('ip'=>$ip));
    $data = curl_exec($ch);
    curl_close($ch);
    preg_match_all('/<i>([a-z\s]+)\:<\/i>\s+<b>(.*)<\/b>/im',$data,$matches,PREG_SET_ORDER);
    if(count($matches)==0)return false;
    $return = array();
    $labels = array(
    'Hostname'          => 'host',
    'IP Country'        => 'country',
    'IP Country Code'   => 'country_code',
    'IP Continent'      => 'continent',
    'IP Region'         => 'region',
    'IP Latitude'       => 'latitude',
    'IP Longitude'      => 'longitude',
    'Organization'      => 'organization',
    'ISP Provider'      => 'isp');
    foreach($matches as $info){
        if(isset($info[2]) && !is_null($labels[$info[1]])){
            $return[$labels[$info[1]]]=$info[2];
        }
    }

    return (count($return))?$return:false;
}
?>
于 2012-09-18T14:47:21.437 に答える
0

必要なものは PHP に組み込まれています。gethostbyname メソッドを使用して IP アドレスを検索し、無料の API (MaxMind など) を使用して場所を検索します。また、PHP には parse_url メソッドがあり、URL から実際のドメイン名を取得するのに役立ちます。より安全な方法が利用できる場合は、shell_exec を使用しないでください。

于 2012-09-18T14:40:58.207 に答える