PHP file_get_contents foreach

时间:2014-04-17 06:18:00

标签: php mysql foreach file-get-contents whois

我在互联网上找到了一个现有脚本,它从WHOIS服务器获取数据并提取相关数据(例如,到期日期,状态)。由于它一直被抛弃,我一直在修改它并创建了我自己的script.php?id=domain.com,它允许我输入任何域名和whois数据, 我遇到的问题是我有我的whois.php文件,我想从我的MySQL数据库中获取一个域列表,并尝试使用(file_get_contents到我的script.php和提取每个域的Expiry / Status数据) foreach)然后用相关信息更新数据库。 我相当确定我已将所有内容编码为“ Foreach ”& “ File_get_contents ”因此我的脚本遇到错误。

  

“警告:第39行/home/user/public_html/mydomain.com/domains/whois.php为foreach()提供的参数无效”

是我收到的错误。

我的 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工作正常,只需让whois.php从我的MySQL数据库中找到每个域的到期日期/状态。

干杯。

1 个答案:

答案 0 :(得分:1)

变化:

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

为:

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

file_get_contents将整个文件作为单个字符串返回。 file将其拆分为一个数组,其中每个元素都是文件的一行。

您还需要在第一个$data循环中处理foreach。否则,您每次都会在循环中覆盖$data,而使用它的代码只会获得最后一个。

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);
}