Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fdusautoir's avatar

Get full url with protocol from domain name

Hi there,

I need to retrieve the correct full url with protocol from a given domain name.


function getFullUrl($domain) 
{ 
    // Some stuff here
     return $url;

} 

Example :


$url = getFullUrl('google.fr'); 

// Expected output :  https://www.google.fr/

How can I achieve that ? I've try with CURL and Http client but didn't succeed in and do not find anything about it in Google.

Thanks for your help.

0 likes
1 reply
fdusautoir's avatar
fdusautoir
OP
Best Answer
Level 11

Ok I found a solution with Guzzle, if anyone else needs it or wants to improve it :

function getFullUrl($domain) 
{
        $request = (new GuzzleHttp\Client)->request('GET', $domain, [
            'allow_redirects' => [
                'track_redirects' => true
            ]
        ]);

        $redirections = $request->getHeaderLine('X-Guzzle-Redirect-History');
        $redirections = explode(',', $redirections);

        return trim(end($redirections));
}
1 like

Please or to participate in this conversation.