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は、この種のものの夢です。これが初めての場合は、Googleの「SimpleXMLPHPチュートリアル」。基本的に:

$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 に答える