File_get_contents()查找不工作的原因


file_get_contents() find why not work

<?
$ip = '95.79.1.36'; //russian ip for test
$str = 'http://ipgeobase.ru:7020/geo?ip='.$ip;
$content = file_get_contents($str);
preg_match_all('#<country>(.*)(</country>)#Usi', $content, $matches); 
$country = $matches[0][0];
preg_match_all('#<city>(.*)(</city>)#Usi', $content, $matches); 
$city = $matches[0][0];
if($country == 'RU'){
echo 'City: '.$city.'';
}else{
echo 'Country: '.$country.'';
}
?>

问题是$country == 'RU',不工作,我的问题是为什么?

谢谢)))

您可能不应该用Regex解析HTML/XHTML/XML。参见:RegEx匹配除XHTML自包含标签以外的打开标签

我建议使用PHP的SimpleXML解析器。下面的方法对我有用:

<?php
$ip = '95.79.1.36'; //russian ip for test
$str = 'http://ipgeobase.ru:7020/geo?ip='.$ip;
$results = simplexml_load_file($str);
$country = $results->ip->country;
$city = $results->ip->city;
if($country == 'RU'){
echo 'City: '.$city.'';
}else{
echo 'Country: '.$country.'';
}
?>

您的服务器可能没有allow_url_fopen (php.ini指令)。无论如何,对于这种特殊情况,您正在寻找的技术是cURL: https://php.net/curl.

我很乐意提供更多关于你的代码的具体解释,甚至提供cURL代码示例,一旦你正确地编辑了你的问题,更多的信息和你的努力的结果