Mesud Mehmed
Mesud Mehmed

Reputation: 11

Multiple ips validation from api [PHP]

I have a api server who resolves people and shows theyr ips as example i resolve the user "r-a-z-e" after the resolving the api returns as answer something like: 91.252.251.44 | 91.253.31.98 and here comes the problem... i need to check this ips if they are valid or not and as example if the api returns 91.252.251.44 | gg.gg.gg.gg to echo only the valid ip so on this example the 91.252.251.44 and if the ips are both valid to show only the first one so if the answer of the api is 91.252.251.44 | 91.253.31.98 to show only 91.252.251.44 the code for call to the api is:

<?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));
?>

And sorry for my bad english...

Upvotes: 0

Views: 52

Answers (1)

uri2x
uri2x

Reputation: 3202

You can use a simple preg_match to find the IP in the response, in the following way:

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
}

Upvotes: 1

Related Questions