2017-03-23 37 views
-1
協議信息

我使用的代碼是:如何使用的file_get_contents如果我沒有關於網站

$url = "example.com"; 
$code = file_get_contents("http://www.".$url); 
if (!$code) { 
    $code = file_get_contents("https://www.".$url); 
} 

我不知道每個URL是否與httphttps開始。我有1,000個網址保存在我的數據庫中。有什麼建議麼?

+1

您必須使用試錯法,因爲您的數據不是URL,而只是可能被解釋爲主機名的字符串。 – arkascha

+1

如果你沒有找到'https'資源你可以回退到'http',你可以嘗試這兩種方法。或相反亦然。 – Federkun

+0

你可以在這些網址上循環,如果條件可以知道哪一個在工作 –

回答

0

This answer有點相關。鑑於你的數據庫相當不完整,因爲URL不完全合格,我傾向於寫一個簡短的例程來更新數據庫以備將來使用。

<?php 
$url = 'example.com'; 
$variations[] = 'http://'.$url; 
$variations[] = 'https://'.$url; 
$variations[] = 'http://www.'.$url; 
$variations[] = 'https://www'.$url; 
foreach($variations as $v){ 
    // Check variation with function from answer linked above 
    if(urlOK($v)){ 
    // code to update database row 
    // or if you don't want to do that 
    $code = file_get_contents($v); 
    } 
} 
+0

謝謝你DaveP。 –

相關問題