0

I've found an existing script on the internet that grabs data from WHOIS servers and extracts the relevent data (Eg. Expiry date, Status). As it was long abandoned I have been modifying it and created my own script.php?id=domain.com which lets me enter any domain and whois data comes up, What I'm having trouble with is I have my whois.php file which I want to grab a list of domains out of my MySQL database and attempt to extract the Expiry/Status data of each domain using (file_get_contents to my script.php and foreach) then update the database with the relevant information. I'm fairly certain I have coded everything apart from the "Foreach" & "File_get_contents" part right therefore my script encounters errors.

"Warning: Invalid argument supplied for foreach() in /home/user/public_html/mydomain.com/domains/whois.php on line 39"

is the error I receive.

Snippet of my whois.php:

include("database.php");
// Select one domain from the database that hasn't been checked yet
$sql = "SELECT domainName from domains WHERE 1 ORDER BY lastChecked ASC";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$domain = $row[0];

if(mysql_num_rows($result) == 0){
    die("No domains found in the database.");
}

// Grab the WHOIS information for the domain selected
// ---------------------------------------------------------------
$domainExt = substr($domain, -3); // grab the domain extension


//var_dump($whois);


$arr = array($content);
foreach($arr as $id) {          
    echo $id, '<br>';           
    $data = file_get_contents('http://codestrike.net/domains/script.php?id='.$domain.'');
    echo $data, '<br>';
}


foreach($data as $whoisline){
    if(strstr($whoisline,"Expiration")){
        $whoisline = str_replace("Expire Date:","",$whoisline);
        $whoisline = trim($whoisline);
        $expiration = substr($whoisline,0,11);
    }

    if(strstr($whoisline,"Status")){
        $statusline = $whoisline;
    }
}

$status = str_replace("Status:","",$statusline);
$status = trim($status);

Script.php?id=domain.com works fine, its just a matter of having the whois.php finding the expiry date/status for each domain out of my MySQL Database.

Cheers.

Barmar
  • 741,623
  • 53
  • 500
  • 612
user3543992
  • 3
  • 1
  • 3
  • 2
    `$data` is a string and you can't pass that as an argument for `foreach` – Shankar Narayana Damodaran Apr 17 '14 at 06:19
  • You want to loop through the database records not through the received file content what you did here is looping through the file get content and not through the records in your db.. – Mike M. Apr 17 '14 at 06:27
  • Offtopic but… is your `$domainExt = substr($domain, -3);` line really safe? What about foo.co.nz or bar.fr ? Shouldn’t you use a regexp instead? – Einenlum Apr 17 '14 at 06:48

1 Answers1

4

Change:

$data = file_get_contents('http://mydomain.com/domains/script.php?id='.$domain.'');

to:

$data = file('http://mydomain.com/domains/script.php?id='.$domain);

file_get_contents returns the entire file as a single string. file splits it up into an array, where each element is a line of the file.

You also need to process $data in the first foreach loop. Otherwise, you're just overwriting $data each time through the loop, and the code that uses it just gets the last one.

include("database.php");
// Select one domain from the database that hasn't been checked yet
$sql = "SELECT domainName from domains WHERE 1 ORDER BY lastChecked ASC";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$domain = $row[0];

if(mysql_num_rows($result) == 0){
    die("No domains found in the database.");
}

// Grab the WHOIS information for the domain selected
// ---------------------------------------------------------------
$domainExt = substr($domain, -3); // grab the domain extension

//var_dump($whois);

$arr = array($content);
foreach($arr as $id) {          
    echo $id, '<br>';           
    $data = file('http://mydomain.com/domains/script.php?id='.$domain);
    var_dump($data); echo '<br>';
    foreach($data as $whoisline){
        if(strstr($whoisline,"Expiration")){
            $whoisline = str_replace("Expire Date:","",$whoisline);
            $whoisline = trim($whoisline);
            $expiration = substr($whoisline,0,11);
        }

        if(strstr($whoisline,"Status")){
            $statusline = $whoisline;
        }
    }

    $status = str_replace("Status:","",$statusline);
    $status = trim($status);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks, Looks like we are getting closer. I've added that in and I'm getting a status code inserted into my domain database for the first domain, the other one still remains blank though – user3543992 Apr 17 '14 at 06:33
  • You need to move all the code that processes `$data` into the first `foreach` loop. Otherwise, it just processes the last domain. – Barmar Apr 17 '14 at 06:35
  • Thanks Barmar, using that code and using some str_replaces for the expiry and status results its all working! Cheers. – user3543992 Apr 17 '14 at 07:32