简体   繁体   English

IP到位置API的PHP正则表达式

[英]PHP Regex for IP to Location API

How would I use Regex to get the information on a IP to Location API 我将如何使用Regex获取有关IP到位置API的信息

This is the API 这是API

http://ipinfodb.com/ip_query.php?ip=74.125.45.100 http://ipinfodb.com/ip_query.php?ip=74.125.45.100

I would need to get the Country Name, Region/State, and City. 我需要获取国家名称,地区/州和城市。

I tried this: 我尝试了这个:

$ip = $_SERVER["REMOTE_ADDR"];
$contents = @file_get_contents('http://ipinfodb.com/ip_query.php?ip=' . $ip . '');
$pattern = "/<CountryName>(.*)<CountryName>/";
preg_match($pattern, $contents, $regex);
$regex = !empty($regex[1]) ? $regex[1] : "FAIL";
echo $regex;

When I do echo $regex I always get FAIL how can I fix this 当我回显$ regex时,我总是会失败,该如何解决呢?

As Aaron has suggested. 正如亚伦所建议的那样。 Best not to reinvent the wheel so try parsing it with simplexml_load_string() 最好不要重新发明轮子,因此请尝试使用simplexml_load_string()对其进行解析

// Init the CURL
$curl = curl_init();

// Setup the curl settings
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);

// grab the XML file
$raw_xml = curl_exec($curl);

curl_close($curl);

// Setup the xml object
$xml = simplexml_load_string( $raw_xml );

You can now access any part of the $xml variable as an object, with that in regard here is an example of what you posted. 现在,您可以将$ xml变量的任何部分作为对象访问,关于这一点,这里是您发布内容的示例。

<Response> 
    <Ip>74.125.45.100</Ip> 
    <Status>OK</Status> 
    <CountryCode>US</CountryCode> 
    <CountryName>United States</CountryName> 
    <RegionCode>06</RegionCode> 
    <RegionName>California</RegionName> 
    <City>Mountain View</City> 
    <ZipPostalCode>94043</ZipPostalCode> 
    <Latitude>37.4192</Latitude> 
    <Longitude>-122.057</Longitude> 
    <Timezone>0</Timezone> 
    <Gmtoffset>0</Gmtoffset> 
    <Dstoffset>0</Dstoffset> 
</Response> 

Now after you have loaded this XML string into the simplexml_load_string() you can access the response's IP address like so. 现在,在将此XML字符串加载到simplexml_load_string()之后,您可以像这样访问响应的IP地址。

$xml->IP;

simplexml_load_string() will transform well formed XML files into an object that you can manipulate. simplexml_load_string()会将格式正确的XML文件转换为可以操纵的对象。 The only other thing I can say is go and try it out and play with it 我唯一能说的就是去尝试并玩

EDIT: 编辑:

Source http://www.php.net/manual/en/function.simplexml-load-string.php 来源http://www.php.net/manual/en/function.simplexml-load-string.php

You really are better off using a XML parser to pull the information. 使用XML解析器提取信息确实更好。

For example, this script will parse it into an array. 例如, 此脚本会将其解析为一个数组。

Regex really shouldn't be used to parse HTML or XML. 正则表达式实际上不应该用于解析HTML或XML。

If you really need to use regular expressions, then you should correct the one you are using. 如果确实需要使用正则表达式,则应更正所使用的正则表达式。 "|<CountryName>([^<]*)</CountryName>|i" would work better. "|<CountryName>([^<]*)</CountryName>|i"会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM