【问题标题】:PHP file_get_contents foreachPHP file_get_contents foreach
【发布时间】:2014-04-17 06:17:59
【问题描述】:

我在 Internet 上找到了一个现有脚本,该脚本从 WHOIS 服务器获取数据并提取相关数据(例如到期日期、状态)。由于它被遗弃了很久,我一直在修改它并创建了我自己的script.php?id=domain.com,它可以让我输入任何域并显示 whois 数据, 我遇到的问题是我有我的 whois.php 文件,我想从我的 MySQL 数据库中获取域列表并尝试使用 (file_get_contents 到我的 script.php 和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 数据库中查找每个域的到期日期/状态。

干杯。

【问题讨论】:

  • $data 是一个字符串,您不能将其作为foreach 的参数传递
  • 你想循环通过数据库记录而不是通过接收到的文件内容你在这里所做的是循环通过文件获取内容而不是通过数据库中的记录..
  • Offtopic 但是……你的$domainExt = substr($domain, -3); 线路真的安全吗?那么 foo.co.nz 或 bar.fr 呢?你不应该使用正则表达式吗?

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


【解决方案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 将其拆分为一个数组,其中每个元素都是文件的一行。

您还需要在第一个 foreach 循环中处理 $data。否则,您只是在每次循环中都覆盖$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);
}

【讨论】:

  • 谢谢,看起来我们越来越近了。我已经添加了它,并且我将一个状态代码插入到我的第一个域的域数据库中,但另一个仍然保持空白
  • 您需要将处理$data 的所有代码移动到第一个foreach 循环中。否则,它只会处理最后一个域。
  • 感谢 Barmar,使用该代码并使用一些 str_replaces 来获取到期和状态结果,一切正常!干杯。
猜你喜欢
  • 1970-01-01
  • 2017-09-14
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 2011-07-09
  • 2012-06-19
  • 2011-06-23
相关资源
最近更新 更多