2

我有以下使用hostip.info上的 api 的脚本。该页面根据 ip 地址解析用户位置的 xml 读数。在我的职能中,除了城市之外,一切都在运作。

preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$city_match);

我已经把它缩小到我preg_match错了,但我不知道如何解决它。这是一个示例 xml 输出:http ://api.hostip.info/?ip=12.215.42.19

<?php
function getCountryCity()
{
    if(isset($_SERVER['REMOTE_ADDR']) && strlen($_SERVER['REMOTE_ADDR']) > 0)  {
        $ipAddr = $_SERVER['REMOTE_ADDR'];
        // verify the IP address
        ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";

        $ipDetail=array();
        // get the XML result from hostip.info
        $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);

        // get the city name inside the node <gml:name> and </gml:name>
        preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$city_match);
        $ipDetail['city'] = $city_match[1]; 

        // get the country name inside the node <countryName> and </countryName>
        preg_match("@<countryName>(.*?)</countryName>@si",$xml,$country_match);
        $ipDetail['country'] = $country_match[1];

        // get the country name inside the node <countryName> and </countryName>
        preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
        $ipDetail['country_code'] = $cc_match[1];

        // return the array containing city, country and country code
        return $ipDetail;
    } else {
        return false;
    }
}

$ipDetail = getCountryCity();
$user_city = $ipDetail['city'];
$user_country = $ipDetail['country'];
$user_cc = $ipDetail['country_code'];

echo $user_country.' ('.$user_cc.')';
echo $user_city;

?>
4

3 回答 3

1

XPATH 是这类东西的梦想。谷歌“SimpleXML PHP 教程”,如果这对您来说是新的。基本上:

$xml = new SimpleXMLElement($yourXML);
$user_city = $xml->xpath('//gml:name/text()');
$user_country= $xml->xpath('//countryName/text()');
$cc= $xml->xpath('//countryAbbrev/text()');

我发现 XPATH 查询比 RegEx 更容易编写。

抱歉,这不能如您所愿直接回答您的问题。试图发表评论,但格式完全搞砸了

于 2012-08-09T14:51:58.837 回答
1
preg_match_all("@<gml:name>(.*?)</gml:name>@si",$xml,$city_match);

只需删除<Hostip>(\s)*并使用 preg_match_all 它将占用所有标签。然后你可以在数组中选择你需要的一个。

于 2012-08-09T14:52:38.233 回答
0
function getCountryCity() {
    if(isset($_SERVER['REMOTE_ADDR']) && strlen($_SERVER['REMOTE_ADDR']) > 0)  {
        $user_ip = $_SERVER['REMOTE_ADDR'];
        $response = file_get_contents('http://api.hostip.info/?ip='.$user_ip);
        $user_details = array();
        $xml = new DOMDocument();
        $xml->loadXml($response);
        $xpath = new DOMXpath($xml);
        $path = '/HostipLookupResultSet/gml:featureMember/Hostip/';

        // create values for array
        $ip = $xpath->evaluate($path . 'ip')->item(0)->nodeValue;
        $city = $xpath->evaluate($path . 'gml:name')->item(0)->nodeValue;
        $countryName = $xpath->evaluate($path . 'countryName')->item(0)->nodeValue;
        $countryAbbrev = $xpath->evaluate($path . 'countryAbbrev')->item(0)->nodeValue;

        // assign values to array
        $user_details['ip'] = $ip;
        $user_details['city'] = $city;
        $user_details['countryName'] = $countryName;
        $user_details['countryAbbrev'] = $countryAbbrev;

        return $user_details;
    } else {
        return false;
    }
}
于 2012-08-09T15:10:30.340 回答