0

I tried to create a script to download favicons from a list of links:

My file link_list.txt looks like the following:

stackoverflow.com
http://google.com
www.gmail.com
facebook.com

I am using the following library to check and download favicons:

Github: FaviconDownloader

My script currently looks like the following:

<?php
require_once 'vendor/autoload.php';
use Vincepare\FaviconDownloader\FaviconDownloader;

$fh = fopen(dirname(__FILE__).DIRECTORY_SEPARATOR . 'link_list.txt','r');
while ($line = fgets($fh)) {

    //if the url has not http:// add it
    if(preg_match("@^http://@i",$line))
        $line = preg_replace("@(http://)+@i",'http://',$line);
    else
        $line = 'http://'.$line;
    echo('URL: ' . $line."\n");
    $favicon = new FaviconDownloader($line);

    if (!$favicon->icoExists) {
        echo "No favicon for ".$favicon->url;
    }

    // get name of url
    $parts = parse_url($line);
    $path_parts = explode('.', isset($parts['host'])?$parts['host']:$parts['path']);

    echo 'Filename: fav-'. $path_parts[0] .'.ico' . "\n";
    $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'fav-'. $path_parts[0] . '.ico';
    file_put_contents($filename, $favicon->icoData);
    echo "Saved to ".$filename."\n\n";
}

fclose($fh);

When I run my script I only get to download the last favicon. In this example it is only facebook:

enter image description here

Any suggestions what is wrong with my script?

I appreciate your replies!

UPDATE

When var_dump($favicon) I get:

class Vincepare\FaviconDownloader\FaviconDownloader#2 (13) {
  public $url =>
  string(21) "http://www.gmail.com
"
  public $pageUrl =>
  NULL
  public $siteUrl =>
  NULL
  public $icoUrl =>
  NULL
  public $icoType =>
  NULL
  public $findMethod =>
  NULL
  public $error =>
  string(31) "Illegal characters found in URL"
  public $icoExists =>
  NULL
  public $icoMd5 =>
  NULL
  public $icoData =>
  NULL
  public $debugInfo =>
  array(1) {
    'document_curl_errno' =>
    int(3)
  }
  protected $httpProxy =>
  NULL
  protected $sslVerify =>
  bool(true)
}

My guess the downloader does not like the \n. Any suggestions how to fix that?

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • When you run the script, do you get any output of "No favicon for ..."? If not, have you done `var_dump($favicon);` to see exactly what it contains? – Patrick Q Jul 11 '18 at 19:19
  • @PatrickQ Please see my update. I guess the downloader does not like the `\n` in my url. Any suggestions how to handle this? – Carol.Kar Jul 11 '18 at 19:25
  • `$line = trim($line);` – Patrick Q Jul 11 '18 at 19:27
  • 1
    Possible duplicate of [How do I eliminate line break from fgets function in PHP?](https://stackoverflow.com/questions/7478250/how-do-i-eliminate-line-break-from-fgets-function-in-php) – Patrick Q Jul 11 '18 at 19:35
  • 1
    i test your code and it is OK but you must add `$line=trim($line);` after `while ($line = fgets($fh)) {` line – Alihossein shahabi Jul 11 '18 at 19:36

1 Answers1

-2

Have you tried opening the icons, they are there so it may work. If it does not work chances are it hasn't downloaded or is corrupt. As for the script, I don't know PHP so i cant help you with the script.