2017-04-26 91 views
0

我試圖獲得用戶輸入的網站標題。PHP - 從用戶網站輸入獲取網站標題

文本輸入:用戶輸入的網站鏈接通過AJAX發送到服務器。 用戶可以輸入任何東西:一個實際存在的鏈接,或只是一個字,或者很奇怪像「po392#* @ 8」

這是我的PHP腳本的部分

  // Make sure the url is on another host 
     if(substr($url, 0, 7) !== "http://" AND substr($url, 0, 8) !== "https://") { 
      $url = "http://".$url; 
     } 

     // Extra confirmation for security 
     if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) { 
      $urlIsValid = "1"; 
     } else { 
      $urlIsValid = "0"; 
     } 

     // Make sure there is a dot in the url 
     if (strpos($url, '.') !== false) { 
      $urlIsValid = "1"; 
     } else { 
      $urlIsValid = "0"; 
     } 

     // Retrieve title if no title is entered 
     if($title == "" AND $urlIsValid == "1") { 

      function get_http_response_code($theURL) { 
       $headers = get_headers($theURL); 
       if($headers) { 
        return substr($headers[0], 9, 3); 
       } else { 
        return 'error'; 
       } 
      } 

      if(get_http_response_code($url) != "200") { 

       $urlIsValid = "0"; 

      } else { 

       $file = file_get_contents($url); 

       $res = preg_match("/<title>(.*)<\/title>/siU", $file, $title_matches); 

       if($res === 1) { 
        $title = preg_replace('/\s+/', ' ', $title_matches[1]); 
        $title = trim($title); 

        $title = addslashes($title); 
       } 

       // If title is still empty, make title the url 
       if($title == "") { 
        $title = $url; 
       } 

      } 
     } 

但是,此腳本中仍然存在錯誤。

完全如果輸入現有的網址爲「https://www.youtube.com/watch?v=eB1HfI-nIRg」當一個不存在的頁面輸入爲「https://www.youtube.com/watch?v=NON-EXISTING」,但它當用戶進入類似「Twitter的行不通工作。 com'(沒有http)或類似'yikes'的東西。

我試圖從字面上寄託都:捲曲的DomDocument ...

的問題是,在輸入無效的鏈接時,Ajax調用永遠不會完成(它使加載),而應該$ urlIsValid =「0」每當發生錯誤時。

我希望有人能幫助你 - 很感激。

彌敦道

+3

對TRUE;返回FALSE什麼? –

+0

也許'preg_match'「尖叫」當'$ file'爲'false'時,顯示警告,(可能的)ajax響應不再是JSON,那麼JS錯誤和加載不會再被停止? –

+0

@PedroLobito我更喜歡在ajax調用中返回字符串,但是你可以只讀'0'爲假,'1'爲真。我在學。 – Nathan

回答

0

你有相對簡單的問題,而是你的解決方案過於複雜,也馬車。

這些是我和你的代碼中發現的問題:

// Make sure the url is on another host 
if(substr($url, 0, 7) !== "http://" AND substr($url, 0, 8) !== "https://") { 
    $url = "http://".$url; 
} 

你會不會請確保可能網址是另一個主機上的方式(也可能是localhost)。你應該刪除這段代碼。

// Make sure there is a dot in the url 
if (strpos($url, '.') !== false) { 
     $urlIsValid = "1"; 
} else { 
     $urlIsValid = "0"; 
} 

此代碼覆蓋它上面的代碼,在那裏你驗證字符串確實是一個有效的URL,因此將其刪除。

附加功能get_http_response_code的定義是毫無意義的。您只能使用file_get_contents獲取遠程頁面的HTML,並根據false檢查它以檢測錯誤。

此外,從您的代碼我得出結論,如果(外部的上下文)變量$title是空的,那麼你將不會執行任何外部提取,所以爲什麼不先檢查它?

總而言之,你的代碼應該是這個樣子:

if('' === $title && filter_var($url, FILTER_VALIDATE_URL)) 
{ 
    //@ means we suppress warnings as we won't need them 
    //this could be done with error_reporting(0) or similar side-effect method 
    $html = getContentsFromUrl($url); 

    if(false !== $html && preg_match("/<title>(.*)<\/title>/siU", $file, $title_matches)) 
    { 
     $title = preg_replace('/\s+/', ' ', $title_matches[1]); 
     $title = trim($title); 
     $title = addslashes($title); 
    } 

    // If title is still empty, make title the url 
    if($title == "") { 
     $title = $url; 
    } 
} 

function getContentsFromUrl($url) 
{ 
    //if not full/complete url 
    if(!preg_match('#^https?://#ims', $url)) 
    { 
     $completeUrl = 'http://' . $url; 
     $result = @file_get_contents($completeUrl); 
     if(false !== $result) 
     { 
      return $result; 
     } 

     //we try with https:// 
     $url = 'https://' . $url; 
    } 

    return @file_get_contents($url); 
} 
+0

謝謝!我之前嘗試過,但我一直在嘗試其他的東西,這就是我最終的結果。如果你輸入'twitter.com',因爲Twitter在'https://'(並且使用'http:// twitter.com',file_get_contents將失敗),它仍然不起作用。你能幫助我嗎?也看到我的其他評論:-) ...哦,你可能忘了PHP使用'AND'而不是'&&' – Nathan

+0

@Nathan現在嘗試 –

+0

@Nathan PHP同時使用'AND'和'&&'但它們有一些不同意思,請參閱http://stackoverflow.com/questions/4502092/php-and-or-keywords –