How to get dyndns via php

0

I would like to get the ip that is in dyndns via PHP .

  • Is it possible to do this ??
  • If yes, where do I start searching?
  • asked by anonymous 05.12.2017 / 13:53

    1 answer

    0

    Use gethostbyname , it will return the IP ( IPv4) corresponding to the address, eg:

    <?php
    $ip = gethostbyname('meu_host_foo_bar.dyndns.org');
    
    echo $ip;
    

    An important detail is that if gethostbyname fails to get the ip address it will return the host itself which may confuse, so you can check it like this:

    <?php
    $hostname = 'meu_host_foo_bar.dyndns.org';
    $ip = gethostbyname($hostname);
    
    if ($ip === $hostname) {
        echo 'Falha ao obter o IP de: ', $hostname;
    } else {
        echo 'IP:', $ip;
    }
    

    Note that you can try using gethostbynamel that will return a list in an array corresponding to the last hostname:

    <?php
    $hostname = 'meu_host_foo_bar.dyndns.org';
    $ips = gethostbynamel($hostname);
    
    if (!$hosts) {
        echo 'Falha ao obter o IP de: ', $hostname;
    } else {
        print_r($hosts);
    }
    

    It will return something like:

    Array
    (
        [0] => 192.0.34.166
    )
    

    That is, depending on the service you can return more than one, then you can check what you need. Failure to do so will return false .

        
    05.12.2017 / 14:03