douyuan3842 2014-04-17 06:17
浏览 125
已采纳

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 2014-04-17 06:22
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低