php - 文件获取内容花费太多时间
问题描述
我在函数中使用 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 - Laravel:在选择框上显示 stdClass 对象值
- java - 为什么 Java TinyB 看不到 hcitool 看到的相同设备?
- java - 如何在 Jenkins 中查看 excel 报告?
- python - 如果值不包含字符串,则用正则表达式替换 Pandas Dataframe 中的值
- sql - 在此示例中如何连接两个表
- java - ListView中的Android项目点击监听器
- javascript - 制作一个在特定时间开始运行的 pubsub 函数
- javascript - 当我尝试在 Google Scripts 中使用数组映射函数时出现语法错误。为什么?
- node.js - 打字稿云功能未编译
- excel - 将求和公式理解为 for 循环中的小计