dtpngq3378499 2016-05-07 20:46
浏览 32

获取域名优惠的策略是什么?

I have a script that parses the page for the link tag, but as there are multiple ways to download a page ( wget, file_get_contents, curl, etc ... ) and there are multiple ways to include a favicon, the script is getting to big.

Is there a concise way to do this? Maybe an API that could be used?

Below is the growing script:

<?php

// Use a direct GET request for debugging, just pass in the domain ( ?domain=test.com )
if($_GET)
{
    $obj = new FaviconFinder();
    $obj->invokeDebug($_GET);
}

class FaviconFinder
{
    // domain before and after redirects
    private $domain;
    private $real_domain;

    // the file and how it was obtained
    private $file_code = '0';
    private $file_page;

    // the favicon and how it was obtained
    private $favicon_code = 'z';
    private $file_favicon;
    private $ext;

    // paths local to server and on the internet (URL)
    private $path_local1 = "../../favicons/";
    private $path_local;
    private $path_internet;

/****************************************************************************************************
invokeTest
****************************************************************************************************/

    public function invokeTest($pipe)
    {
        exec('wget ' . $pipe['domain'] . ' -O ../sites/temp.html 2>&1', $output);
        print_r($output);
    }

/****************************************************************************************************
invokeDebug
****************************************************************************************************/

    public function invokeDebug($pipe)
    {

        echo "<br><br> domain: " . $pipe['domain'] . "";
        $pipe = $this->invoke($pipe);
        echo "<br><br> real_domain: " . $this->real_domain . "";
        echo "<br><br> file_code | " . $this->file_code;
        echo "<br><br> favicon_code | " . $this->favicon_code;
        echo "<br><br> favicon_path | " . $this->path_internet;
        echo "<br><br> favicon_file | " . $this->file_favicon;
        echo "<br><br> favicon_file type | " . gettype($this->file_favicon);
        echo "<br><br> favicon_file length | " . strlen($this->file_favicon);
        echo "<br><br> IMAGE: ";
        if ($this->file_favicon)
        {
            echo "<br><br> path_local | " . $this->path_local . "<br><br>";
            $file64 = base64_encode($this->file_favicon);
            echo "<img src= 'data:image/" . $this->ext . ";base64," . $file64 . "'></img>";
        }
        echo "<br><br>";
    }

/****************************************************************************************************
invoke
****************************************************************************************************/

    public function invoke( $pipe )
    {
        $domain = $pipe['domain'];
        if ( $this->pageFound($domain) && $this->linkFound() && $this->faviconFoundFromLink() )
        {
            $pipe = $this->saveFavicon($pipe);
            $pipe['favicon'] = $this->path_internet;
            $pipe['favicon_local'] = $this->path_local;
        } else {
            $pipe['favicon'] = 'NULL';
            $pipe['favicon_local'] = 'image_generic.png';
        }
        $pipe['method'] = $this->file_code . $this->favicon_code;
        return $pipe;
    }

/****************************************************************************************************
pageFound - uses the facade pattern to find a page and record how it was found
****************************************************************************************************/

    private function pageFound ($domain) 
    {
        return $this->pageFoundCurl($domain) || $this->pageFoundGet($domain);
    }

    // wget is another way to get past login page
    // https://stackoverflow.com/questions/1324421/how-to-get-past-the-login-page-with-wget

    // uses curl_exec to retreive a page
    private function pageFoundCurl ($domain)
    {
        $types = array(
            "curl - 4"=>'https://www.' . $domain, 
            "curl - 3"=>'http://www.' . $domain,
            "curl - 6"=>'https://' . $domain,
            "curl - 5"=>'http://' . $domain,

            // returned 302 errors for test.com
            "curl - 1"=>$domain, 
            "curl - 2"=>'www.' . $domain
        );

        foreach ($types as $key => $value) {
            $this->file_page = $this->curlExec($value, true);
            if ($this->file_page)
            {
                $this->file_code = $key;
                return true;
            }
        }
        return false;
    }

    // uses file_get_contents to retreive a page
    private function pageFoundGet( $domain )
    {
        $types = array(
            "file_get - 3"=>'http://www.' . $domain,
            "file_get - 4"=>'https://www.' . $domain, 
            "file_get - 5"=>'http://' . $domain,
            "file_get - 6"=>'https://' . $domain,
            "file_get - 1"=>$domain, 
            "file_get - 2"=>'www.' . $domain
        );

        foreach ($types as $key => $value) {
            if ($this->file_page = $this->fileGetContents( $value ))
            {
                $this->file_code = $key;
                return true;
            }
        }
        return false;
    }

/****************************************************************************************************
linkFound
****************************************************************************************************/

    private function linkFound()
    {
        $domain = $this->real_domain;
        $regex = '#<link\s+(?=[^>]*rel=(?:\'|")(?:shortcut\s)?icon(?:\'|")\s*)(?:[^>]*href=(?:\'|")(.+?)(?:\'|")).*>#i';
        $link_found = preg_match( $regex , $this->file_page, $matches );
        if($link_found === 1)
        {
            $path = $matches[1];

            // handles ( // )
            if ( $path[0] === '/' && $path[1] === '/' )
            {
                $this->favicon_code = 'a';
                $this->path_internet = 'http:' . $path;
            }

            // handles ( / )
            else if( $path[0] === '/' )
            {
                $this->favicon_code = 'b';
                $this->path_internet = 'http://www.' . $domain . $path;
            }

            // handles ( http:// || https:// )
            else if ( substr($path, 0, 4) === 'http' )
            {
                $this->favicon_code = 'c';
                $this->path_internet = $path;
            }

            // difference between b and d?
            else
            {
                $this->favicon_code = 'd';
                $this->path_internet = 'http://www.' . $domain . '/' . $path;
            }
        }
        else
        {
            $default_location = 'http://www.' . $domain . '/favicon.ico';

            /*
            if( $this->faviconFound($default_location) )
            {
                $this->favicon_code = 'e';
                $this->path_internet = $default_location;
            }
            */

            $this->path_internet = null;
            $this->favicon_code = 'g';
            return false;

        }
        return true;
    }

/****************************************************************************************************
faviconFound
****************************************************************************************************/

    private function faviconFoundFromLink () 
    {
        $this->file_favicon = $this->faviconFoundFacade( $this->path_internet );
        return $this->file_favicon ? true : false;
    }

    private function faviconFound ($default_location) 
    {
        $this->file_favicon = $this->faviconFoundFacade( $default_location );
        return $this->file_favicon ? true : false;
    }

/****************************************************************************************************
More
****************************************************************************************************/

    private function faviconFoundFacade($url)
    {
        return $this->faviconFoundCurl($url) ;  
    }

    private function faviconFoundExec($url)
    {
        exec('wget ' . $url . ' -O ../sites/temp.html 2>&1', $output);
    }

    private function faviconFoundGet($url)
    {
        return @file_get_contents( $url );
    }

    // make less than 10 characters equate to false so I don't save bogus files
    // prisonexp.org does this
    // bestbuy.com does similar
    private function faviconFoundCurl($url)
    {
        $temp = $this->curlExec( $url, false );
        if($temp === false)
        {
            return false;
        }
        if(strlen($temp) < 20) 
        {
            return false;
        }
        return $temp;
    }

/****************************************************************************************************
saveFavicon
****************************************************************************************************/

    public function saveFavicon( $pipe )
    {

        // this will remove any query parameters on the favicon link
        // and create a valid file name from the real domain
        $arr = parse_url($this->path_internet);
        $this->ext = pathinfo($arr['path'], PATHINFO_EXTENSION);
        $name = str_replace('.', '_', $this->real_domain);

        // add the extension if it exists, verify you need to to do this
        if ($this->ext) {
            $name = $name . "." . $this->ext;
        }

        // finally save it
        file_put_contents($this->path_local1 . $name, $this->file_favicon);
        $this->path_local = $name;
        return $pipe;
    }

/****************************************************************************************************
helper and wrapper functions
****************************************************************************************************/

    // curl_exec wrapper    
    private function curlExec ($url, $set)
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
        ));
        $temp = curl_exec($curl);
        if ($set) $this->setRealDomain($curl);
        curl_close($curl);
        return $temp;
    }
    private function setRealDomain ($curl)
    {
        $url = curl_getinfo( $curl )['url'];
        $url = parse_url($url);
        $url = $url['host'];
        $this->real_domain = preg_replace('#^www\.(.+\.)#i', '$1', $url);
    }

    // deprecated as curl can do everything I need, just in case though
    // https://stackoverflow.com/questions/
    // 6009284/how-do-i-ignore-a-moved-header-with-file-get-contents-in-php
    private function fileGetContents($value)
    {
        $opts = array(
            'http'=>array(
                'follow_location' => true,
                'max_redirects' => 20
            )
        );
        $context = stream_context_create($opts);
        return @file_get_contents( $value, false, $context );
    }

/****************************************************************************************************
removed
****************************************************************************************************/

    private function removed ()
    {
        $res = preg_match('#(.*?)([^\.]*)(\.)([^\.]*)$#', $domain, $matches);
        if($matches[1])
        {
            $main = $matches[2] . $matches[3] . $matches[4]; 
            $default_location = 'http://www.' . $main . '/favicon.ico';
            $this->file_favicon = @file_get_contents( $default_location );
            if( $this->file_favicon )
            {
                $this->path_internet = $default_location;
                $this->favicon_code = 'f';
                return true;
            }
        }        
    }

}

Here is one API for the front-side.

To check favicon using Google API

  • 写回答

1条回答 默认 最新

  • douyu9159 2016-05-07 21:44
    关注

    There is no strategy or API for favicons. Parse the HTML, look for:

    <link rel="shortcut icon" href="...">
    

    or just:

    <link rel="icon" href="...">
    

    and extract the value of the href attribute.

    If no such tag exists (or the referenced icon is not there) then check for /favicon.ico (this is how everything started in 1999, on Internet Explorer 5).

    Additionally, iOS (and some versions of Android) searches for extra <link> elements having rel="apple-touch-icon" or rel="apple-touch-icon-precomposed".

    Everything else is just guessing and speculations.

    See also: https://en.wikipedia.org/wiki/Favicon#History

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度