2017-04-26 94 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 –