php解析来自url的xml

时间:2013-12-13 20:52:54

标签: php xml parsing

http://api.hostip.info/?ip=87.14.94.152

从这个链接(xml)我试图检索国家名称和国家/地区简称如下:

$url = 'http://api.hostip.info/?ip=87.14.94.152';
$xml = simplexml_load_file($url) or die("feed not loading");
$Country = $xml->gml['featureMember']->Hostip->countryName;
echo $Country;
echo 'BREAK HTML';
echo "-----";
echo "// "; var_dump($xml); echo " //";

但是$ Country是空白的,有什么想法吗?

提前致谢

6 个答案:

答案 0 :(得分:1)

试试这个:

$url = 'http://api.hostip.info/?ip=87.14.94.152';
$xml = simplexml_load_file($url) or die("feed not loading");

$fm=$xml->xpath('//gml:featureMember');
print_r($fm[0]->Hostip->countryName);

答案 1 :(得分:1)

countryName节点嵌套在更深层次。您可以使用children()方法访问带冒号的属性。以下是获取国家/地区名称的方法:

$countryName = (string) $xml->children('gml', true)
                            ->featureMember->children('', true)
                                ->Hostip->countryName; // => ITALY

您还可以使用XPath表达式来检索国家/地区名称。这更容易:

$hostip = $xml->xpath('//Hostip'); 
$countryName = $hostip[0]->countryName;       // => ITALY
$countryAbbrev = $hostip[0]->countryAbbrev;   // => IT

答案 2 :(得分:1)

制作xml的本地副本,它将起作用。刚试过这个并得到了数据:

$url = 'http://api.hostip.info/?ip=87.14.94.152';
$data = file_get_contents($url);
$xml = simplexml_load_file($data) or die("feed not loading");

答案 3 :(得分:1)

     protected function getCountryNameFromIP()
     {

        $ip  = $_SERVER['REMOTE_ADDR'];
        $opts = array(
        'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar\r\n"
           )
        );

        $context = stream_context_create($opts);
        $answerIP = @file_get_contents("http://api.ipinfodb.com/v3/ip-country/?key=4b585e37503a519a408dc17878e6ec04fa963e1b946c567722538d9431c2d5cb&format=xml&ip=$ip" ,false,$context);

        if(isset($answerIP) && $answerIP !="")
        {
            $theResJ            =   simplexml_load_string($answerIP);
            $last_login_ip_cn   =   $theResJ->countryName;


            /**
            *   $last_login_ip_cc   =   $theResJ->countryCode;
            *   $last_login_ip_rc   =   $theResJ->regionCode;
            *   $last_login_ip_rn   =   $theResJ->regionName;
            *   $last_login_ip_cp   =   $theResJ->cityName;
            *   $last_login_ip_lat  =   $theResJ->latitude;
            *   $last_login_ip_lng  =   $theResJ->longitude;
            *   $last_login_zip_code=   $theResJ->zipCode;
            */
        }
        else
        {
            $last_login_ip_cn   =   "";
            /**
            *   $last_login_ip_cc   =   "";
            *   $last_login_ip_rc   =   "";
            *   $last_login_ip_rn   =   "";
            *   $last_login_ip_cp   =   "";
            * $last_login_ip_lat    =   "";
            *   $last_login_ip_lng  =   "";
            *   $last_login_zip_code=   "";
            */
        }

        return $last_login_ip_cn;
       }

I hope it helps you

答案 4 :(得分:1)

我同意。我只是尝试了两个答案并取得了不错的成绩。这是我刚刚运行的测试代码:

<?php
$url = 'http://api.hostip.info/?ip=87.14.94.152';
// $data = file_get_contents($url);
$xml = simplexml_load_file($url) or die("feed not loading");
// $Country = $xml->gml['featureMember']->Hostip->countryName;
// echo $Country;
echo 'BREAK HTML';
echo "-----";
echo "// "; var_dump($xml); echo " //<br/>";
?><br/><?php
var_dump($xml->gml);
?><br/><?php
print_r($xml);
?><br/><?php
var_dump((string) $xml->gml->featureMember->Hostip->countryName);
?><br/><?php
echo $xml->gml['featureMember']->Hostip->countryName;
$Country = (string) $xml->children('gml', true)
                            ->featureMember->children('', true)
                                ->Hostip->countryName; // => ITALY
echo $Country;
$fm=$xml->xpath('//gml:featureMember');
print_r($fm[0]->Hostip->countryName);

以下是结果输出:

BREAK HTML ----- // object(SimpleXMLElement)#1(1){[“@attributes”] =&gt; array(1){[“version”] =&gt; string(5)“1.0.1”}} //

object(SimpleXMLElement)#2(0){} SimpleXMLElement对象([@attributes] =&gt;数组([版本] =&gt; 1.0.1)) string(0)“” ITALYSimpleXMLElement对象([0] =&gt; ITALY)

答案 5 :(得分:1)

尝试这不是一个答案,但是,只是另一种方式来做同样的事情:

$data = file_get_contents('http://api.hostip.info/get_html.php?ip=87.14.94.152&position=true');

$arrayofdata = explode("\n", $data);
$country = explode(":", $arrayofdata[0]);
$count = explode(" ", $country[1]);

echo "Country Name: ".$count[1]."</br>";    //Prints ITALY
echo "Country Abbv: ".trim($count[2],"()"); //Prints IT

position = true url part,它只是为了你想要检索坐标。

干杯;)