从api [PHP]进行多个ip验证


Multiple ips validation from api [PHP]

我有一个api服务器解析人并显示他们的ip作为示例,我在解析api返回后解析用户"r-a-z-e"作为答案:91.252.251.44 | 91.253.31.98,这里出现了问题…我需要检查这个IP是否有效,例如,如果API返回91.252.251.44 | gg.gg.gg.gg只回显有效的IP,那么在这个例子中,91.252.251.44,如果IP都是有效的,只显示第一个,所以如果API的答案是91.252.251.44 | 91.253.31.98只显示91.252.251.44,调用API的代码是:

<?php       
if(!empty($_GET["username"])):
$username = trim(htmlspecialchars(strtolower($_GET["username"])));
    $curl = curl_init();    
            curl_setopt($curl, CURLOPT_URL, "http://www.heretheapilink.com/skype.php?username=".$username);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
            $ip = curl_exec($curl);
            curl_close($curl);
          echo htmlspecialchars(trim($ip));
?>

很抱歉我的英语不好…

您可以使用简单的preg_match在响应中查找IP,方法如下:

if (preg_match('/(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/', $ip, $matches))
{
  $realIP = $matches[0];
  // Do whatever you want with the $realIP here
}