首页 > 解决方案 > 文件获取内容花费太多时间

问题描述

我在函数中使用 file_get_content 来预览 url。调用它的函数在固定间隔后调用。但问题是文件获取内容太慢,阻碍了进一步的处理。任何人请告诉我如何固定 file_get_content 以便它不会阻止任何进程。

function showtext(){
$string = $row["user_msg"];
if (filter_var($string, FILTER_VALIDATE_URL))
{ 
$main_url = $string;
$context = stream_context_create(array('http' => array('header'=>'Connection: close')));
@$str = file_get_contents($main_url, false, $context);
if(strlen($str)>0)
{
$str = trim(preg_replace('/\s+/', ' ', $str));
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title);
}
$d = new DomDocument();
@$d->loadHTML($str);
$xp = new domxpath($d);
foreach ($xp->query("//meta[@property='og:image']") as $el)
{
 $l2=parse_url($el->getAttribute("content"));
 if($l2['scheme'])
{
 $img[0]=$el->getAttribute("content");
}
 else
{
 echo "something is wrong !..";
 }
}
$img_display ='';
if(!empty($img)){
$img_display = '<img style="width:50px; height:45px; display:inline-block; float:left; border-radius:2%;" src="'.$img[0].'">';
}
$title_display ='';
if(!empty($title)){ 
$title_display = '<h5 style="padding-left:2px; color:brown; display:inline-block; font-size:12px;">'.$title[1].'</h5>';
}
$output= '<div style="width:98%; margin:2px; border:1px solid #ccc; border-radius:3px;">'.$img_display.'<a href="'.$main_url.'" style="text-decoration:none; width:165px; margin:0px;color:black; display:inline-block;" target="blank">'.$title_display.'</a></div>                              <p>'.$row["user_msg"].'</p>';
}
else{
    $output= '<div>'.$row['user_msg'].'</div>';
}
}
echo $output;
return $output;
}
}

标签: php

解决方案


推荐阅读