我正在尝试使用curl的应用程序上file_get_contents()
工作allow_url
。从远程站点获取数据。我使用以下代码获取数据:
$url="http://api.hostip.info/get_html.php?ip=182.188.193.238";
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
print_r($result);
当我打印时我得到了Country: PAKISTAN (PK) City: Lahore IP: 182.188.193.238
。我想从这个字符串中获得国家$data = json_decode($result,true);
。Json解码返回空结果。我认为唯一的方法是打破那个字符串?感谢您提前提供任何提示。我想从结果中获取国家/地区名称。
采用 preg_match
preg_match('/Country: (?P<country>\w+)/', $result, $matches);
print_r($matches);
输出:
Array
(
[0] => Country: PAKISTAN
[country] => PAKISTAN
[1] => PAKISTAN
)
所以你会得到国名
$countryName = $matches['country'];
就像@hardik solanki所说的,还有JSON端点get_json.php
。
$url = "http://api.hostip.info/get_json.php?ip=182.188.193.238";
要从JSON响应中获取国家/地区,请使用以下命令:
$response = json_decode($result);
$countryName = $response->country_name;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句