2014-04-17 68 views
0

我在互聯網上發現了一個從WHOIS服務器獲取數據並提取相關數據(例如到期日期,狀態)的現有腳本。由於它被遺棄了很長時間,我一直在修改它,並創建了我自己的script.php?id=domain.com,它允許我輸入任何域名和whois數據, 我遇到麻煩的是我有我的whois.php文件,我想要列表的域出於我的MySQL數據庫,並試圖使用(file_get_contents到我的script.php和foreach)提取每個域的Expiry/Status數據,然後用相關信息更新數據庫。 我很確定我已經編碼了一切,除了「Foreach」&「File_get_contents」部分權限,因此我的腳本遇到錯誤。PHP file_get_contents foreach

「警告:()在/ home提供的foreach無效參數/ 用戶 /public_html/mydomain.com/domains/whois.php第39行上」

是我收到錯誤。

的片段我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數據庫。

乾杯。

+2

'$ data'是一個字符串,你不能傳遞作爲一個參數'foreach' –

+0

您可以通過數據庫中的記錄不是通過你在這裏做什麼,接收的文件的內容是通過循環要循環文件獲取內容,而不是通過您的數據庫中的記錄.. –

+0

Offtopic,但...是你的'$ domainExt = substr($域,-3);'線真的很安全嗎?那麼foo.co.nz或bar.fr呢?你不應該使用正則表達式嗎? – Einenlum

回答

0

變化:

$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); 
} 
+0

謝謝,看起來我們正在接近。我已經添加了,並且我得到的狀態代碼插入到第一個域的域數據庫中,另一個仍然爲空,儘管 – user3543992

+0

您需要將處理'$ data'的所有代碼移動到第一個「foreach '循環。否則,它只處理最後一個域。 – Barmar

+0

感謝Barmar,使用該代碼和使用一些str_replaces過期和狀態結果它的所有工作!乾杯。 – user3543992