how to get favicon with goutte php

Solutions on MaxInterview for how to get favicon with goutte php by the best coders in the world

showing results for - "how to get favicon with goutte php"
Ben
09 Sep 2018
1function getFavicon($url){
2    # make the URL simpler
3    $elems = parse_url($url);
4    $url = $elems['scheme'].'://'.$elems['host'];
5
6    # load site
7    $output = file_get_contents($url);
8
9    # look for the shortcut icon inside the loaded page
10    $regex_pattern = "/rel=\"shortcut icon\" (?:href=[\'\"]([^\'\"]+)[\'\"])?/";
11    preg_match_all($regex_pattern, $output, $matches);
12
13    if(isset($matches[1][0])){
14        $favicon = $matches[1][0];
15
16        # check if absolute url or relative path
17        $favicon_elems = parse_url($favicon);
18
19        # if relative
20        if(!isset($favicon_elems['host'])){
21            $favicon = $url . '/' . $favicon;
22        }
23
24        return $favicon;
25    }
26
27    return false;
28}
29