Graby\Extractor\HttpClient::getUserAgent PHP Method

getUserAgent() private method

Based on the config, it will try to find a UserAgent from an host. Otherwise it will use the default one.
private getUserAgent ( string $url, array $httpHeader = [] ) : string
$url string Absolute url
$httpHeader array Custom HTTP Headers from SiteConfig
return string
    private function getUserAgent($url, $httpHeader = array())
    {
        $ua = $this->config['ua_browser'];
        if (!empty($httpHeader['user-agent'])) {
            $this->logger->log('debug', 'Found user-agent "{user-agent}" for url "{url}" from site config', array('user-agent' => $httpHeader['user-agent'], 'url' => $url));
            return $httpHeader['user-agent'];
        }
        $host = parse_url($url, PHP_URL_HOST);
        if (strtolower(substr($host, 0, 4)) == 'www.') {
            $host = substr($host, 4);
        }
        $try = array($host);
        $split = explode('.', $host);
        if (count($split) > 1) {
            // remove first subdomain
            array_shift($split);
            $try[] = '.' . implode('.', $split);
        }
        foreach ($try as $h) {
            if (isset($this->config['user_agents'][$h])) {
                $this->logger->log('debug', 'Found user-agent "{user-agent}" for url "{url}" from config', array('user-agent' => $this->config['user_agents'][$h], 'url' => $url));
                return $this->config['user_agents'][$h];
            }
        }
        $this->logger->log('debug', 'Use default user-agent "{user-agent}" for url "{url}"', array('user-agent' => $ua, 'url' => $url));
        return $ua;
    }