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

PHP-从用户站点输入获取网站标题,php,ajax,curl,title,file-get-contents,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://") {

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

文本输入:用户输入的网站链接通过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;
                }

            }
        }
//确保url位于另一台主机上
if(substr($url,0,7)!=“http://”和substr($url,0,8)!=“https://”){
$url=“http://”$url;
}
//额外的安全确认
if(过滤器变量($url,过滤器验证,过滤器标志,需要主机)){
$urlIsValid=“1”;
}否则{
$urlIsValid=“0”;
}
//确保url中有一个点
if(strpos($url,'.')!==false){
$urlIsValid=“1”;
}否则{
$urlIsValid=“0”;
}
//如果未输入标题,则检索标题
如果($title==”和$urlIsValid==“1”){
函数get\u http\u response\u code($theURL){
$headers=get_headers($theURL);
如果($headers){
返回substr($headers[0],9,3);
}否则{
返回“错误”;
}
}
如果(获取http响应代码($url)!=“200”){
$urlIsValid=“0”;
}否则{
$file=文件获取内容($url);
$res=preg_match(“/(.*)/siU“,$file,$title_matches);
如果($res==1){
$title=preg_replace('/\s+/',''$title_匹配[1]);
$title=修剪($title);
$title=addslashes($title);
}
//如果title仍然为空,请将title设置为url
如果($title==“”){
$title=$url;
}
}
}
但是,此脚本中仍然存在错误

如果将现有url输入为“”,并且将不存在的页面输入为“”,则可以正常工作,但当用户输入类似“twitter.com”(不含http)或类似“yikes”的内容时,则无法正常工作

我尝试了所有的事情:卷曲,卷曲

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

我希望有人能帮助你,非常感谢

Nathan

你的问题相对简单,但你的解决方案太复杂,而且有问题

以下是我在您的代码中发现的问题:

// Make sure the url is on another host
if(substr($url, 0, 7) !== "http://" AND substr($url, 0, 8) !== "https://") {
     $url = "http://".$url;
}
您无法通过这种方式确保可能的url位于另一台主机上(它可能是
localhost
)。您应该删除此代码

// Make sure there is a dot in the url
if (strpos($url, '.') !== false) {
        $urlIsValid = "1";
} else {
        $urlIsValid = "0";
}
此代码覆盖其上方的代码,您可以验证该字符串是否确实是有效的
URL
,因此将其删除

附加函数
get\u http\u response\u code
的定义毫无意义。您只能使用
file\u get\u 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);
}
if(“”==$title&&filter\u var($url,filter\u VALIDATE\u url))
{
//@这意味着我们不需要警告,所以会抑制警告
//这可以通过错误报告(0)或类似的副作用方法完成
$html=getContentsFromUrl($url);
if(false!=$html&&preg\u匹配(“/(.*)/siU“,$file,$title\u匹配))
{
$title=preg_replace('/\s+/',''$title_匹配[1]);
$title=修剪($title);
$title=addslashes($title);
}
//如果title仍然为空,请将title设置为url
如果($title==“”){
$title=$url;
}
}
函数getContentsFromUrl($url)
{
//如果不是完整/完整的url
如果(!preg_match(“#^https?:/#ims',$url))
{
$completeUrl='http://'。$url;
$result=@file\u get\u contents($completeUrl);
if(false!==$result)
{
返回$result;
}
//我们尝试使用https://
$url='https://'。$url;
}
返回@file\u get\u内容($url);
}

任何与
true
false
相反的内容?可能是
preg_match
$file
false
时,“尖叫”,显示警告,(可能)ajax响应不再是JSON,而不是JS错误,加载也不再停止?@PedroLobito我更喜欢在ajax调用中返回字符串,但是是的,你可以把“0”读作假,把“1”读作真。我在学习。@ConstantinGALBENU真棒!这解决了一些问题。但是现在的问题是,正如您在代码中看到的,如果缺少传输协议,我会添加“HTTP://”。但例如twitter.com在HTTPS://,现在它只适用于HTTP://链接,而不适用于HTTPS://链接。如果我进入twitter.com,它不起作用,但它确实起作用,例如。谢谢!我以前试过,但我一直在尝试其他事情,这就是我最终的结果。如果您输入
twitter.com
,它仍然不起作用,因为twitter位于
https://
(并且使用
http://twitter.com
,文件获取内容将失败)。你能帮我吗?另请参阅我的其他评论:-)。。。哦,你可能忘了PHP使用
而不是
&&
@nathantrynow@NathanPHP同时使用
以及
&&
,但它们的含义略有不同,看,如果Twitter验证HTTP头/用户,我想您可以使用php cUrl库-agents@Constantin谢谢你更新你的答案,我学到了一些新的东西!(现在我想知道我只使用和/或在脚本中使用是否不好)