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 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度