<?php

namespace CascadeEnergy\ServiceDiscovery\Consul;

use CascadeEnergy\ServiceDiscovery\ServiceDiscoveryClientInterface;

class ConsulDns implements ServiceDiscoveryClientInterface
{
    private $lookupService;

    public function __construct(callable $lookupService = null)
    {
        $this->lookupService = $lookupService;

        if (is_null($lookupService)) {
            $this->lookupService = "dns_get_record";
        }
    }

    public function getServiceAddress($serviceName, $version = null)
    {
        $dnsEntry = "$serviceName.service.consul";

        if (!empty($version)) {
            $dnsEntry = "$version.$dnsEntry";
        }

        $resultList = call_user_func($this->lookupService, $dnsEntry, DNS_SRV);

        if (empty($resultList)) {
            return false;
        }

        $ipAddress = '';
        $port = '80';

        foreach ($resultList as $result) {
            if (isset($result['host'])) {
                $ipAddress = $result['host'];
            }

            if (isset($result['port'])) {
                $port = $result['port'];
            }
        }

        return "$ipAddress:$port";
    }
}

<?php

namespace CascadeEnergy\ServiceDiscovery\Consul;

use CascadeEnergy\ServiceDiscovery\ServiceDiscoveryClientInterface;

class ConsulDns implements ServiceDiscoveryClientInterface
{
    private $lookupService;

    public function __construct(callable $lookupService = null)
    {
        $this->lookupService = $lookupService;

        if (is_null($lookupService)) {
            $this->lookupService = "dns_get_record";
        }
    }

    public function getServiceAddress($serviceName, $version = null)
    {
        $dnsEntry = "$serviceName.service.consul";

        if (!empty($version)) {
            $dnsEntry = "$version.$dnsEntry";
        }

        $resultList = call_user_func($this->lookupService, $dnsEntry, DNS_SRV);

        if (empty($resultList)) {
            return false;
        }

        $ipAddress = '';
        $port = '80';

        foreach ($resultList as $result) {
            if (isset($result['target'])) {
                $ipAddress = $result['target'];
            }

            if (isset($result['port'])) {
                $port = $result['port'];
            }
        }

        return "$ipAddress:$port";
    }
}

Fix ip -> target bug
Fix ip -> target bug
