douyuan3842
douyuan3842
2014-04-17 06:17

PHP file_get_contents foreach

已采纳

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongxuying7583 dongxuying7583 7年前

    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);
    }
    
    点赞 评论 复制链接分享