skip to Main Content

So I found a solution to a problem I was having, (how to determine a domain protocol) How to find the domain is whether HTTP or HTTPS (with or without WWW) using PHP?

Below are two versions of my code;

The first doesn’t work as expected it only echoes out my domains.

<?php
$url_list = file('urls.txt');
    foreach($url_list as $url)
    {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_exec($ch);

    $real_url =  curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

    echo $real_url;
}

?>

The Second version of my code gives me an error of how I am supplied my foreach statement…

<?php


      $fn = fopen("urls.txt","r");

    while(! feof($fn))  {
    $url_list = fgets($fn);

    foreach($url_list as $url)
    {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_exec($ch);

    $real_url =  curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

    echo $real_url;
    #echo $result;
    fclose($fn);
        }
    }

?>

What can I be doing wrong??

Expected results is this, but when reading the domains from a file;

code: reference –> How to find the domain is whether HTTP or HTTPS (with or without WWW) using PHP?

<?php


      $url_list = ['facebook.com','google.com'];

foreach($url_list as $url){

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_exec($ch);

    $real_url =  curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    echo $real_url;//add here your db commands

}

?>

Output:

test@linux: php domain-fuzzer.php
https://www.facebook.com/http://www.google.com/#

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution, just incase anyone would find such an issue here is the code:

    
    <?php   
        $fn = file_get_contents("urls.txt");
        $url_list = explode(PHP_EOL, $fn);
    
        foreach($url_list as $url)
        {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
        curl_exec($ch);
    
        $real_url =  curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    
        echo  nl2br("$real_url n");
        }
    ?>   
    
    

  2. By default fgets returns a single current line from the file not an array of all lines of the file.
    So, your code will be:

    <?php
       $fn = fopen("urls.txt","r");
    
    while(! feof($fn))  {
    $url = fgets($fn);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; 
    rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_exec($ch);
    
    $real_url =  curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    
    echo $real_url;
    #echo $result;
    fclose($fn);
    }
    
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search