PHP - 从用户站点输入获取网站标题

时间:2017-04-26 18:30:24

标签: php ajax curl title file-get-contents

我正在尝试获取用户输入的网站标题。

文本输入:用户输入的网站链接通过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'之类的内容时

我尝试了字面意思:cUrl,DomDocument ......

问题是当输入无效链接时,ajax调用永远不会完成(它会继续加载),而每当发生错误时它应该是$ urlIsValid =“0”。

我希望有人可以帮助你 - 这是值得赞赏的。

1 个答案:

答案 0 :(得分: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);
}