在PHP中,你可以使用多种方法来获取网页的标题和内容信息。一个常见的方法是使用cURL库来发送HTTP请求,然后使用DOM解析器(如DOMDocument)来解析HTML内容。以下是一个示例代码,展示如何实现一个简单的接口来获取网页的标题和内容信息。
$url = filter(get('url'));
if (!$url) {
return json(['code' => -1, 'msg' => '链接不能为空!']);
} else if (get_curl($url) === false) {
return json(['code' => -1, 'msg' => '提交的网址无法访问!']);
}
$data = get_curl($url);
//Title
preg_match('/<TITLE>([\w\W]*?)<\/TITLE>/si', $data, $Title);
if (!empty($Title[1])) {
$title = $Title[1];
}
//Keywords
preg_match('/<META\s+name="keywords"\s+content="([\w\W]*?)"/si', $data, $Keywords);
if (empty($Keywords[1])) {
preg_match("/<META\s+name='keywords'\s+content='([\w\W]*?)'/si", $data, $Keywords);
}
if (empty($Keywords[1])) {
preg_match('/<META\s+content="([\w\W]*?)"\s+name="keywords"/si', $data, $Keywords);
}
if (empty($Keywords[1])) {
preg_match('/<META\s+http-equiv="keywords"\s+content="([\w\W]*?)"/si', $data, $Keywords);
}
if (!empty($Keywords[1])) {
$keywords = $Keywords[1];
}
//Description
preg_match('/<META\s+name="description"\s+content="([\w\W]*?)"/si', $data, $Description);
if (empty($Description[1])) {
preg_match("/<META\s+name='description'\s+content='([\w\W]*?)'/si", $data, $Description);
}
if (empty($Description[1])) {
preg_match('/<META\s+content="([\w\W]*?)"\s+name="description"/si', $data, $Description);
}
if (empty($Description[1])) {
preg_match('/<META\s+http-equiv="description"\s+content="([\w\W]*?)"/si', $data, $Description);
}
if (!empty($Description[1])) {
$description = $Description[1];
}
if ($Title[1] !== "") {
dataLog('URL['.$url.发布单页信息获取成功!','1','1');
$result = array('code'=>0,'msg'=>'获取成功!','title'=>$title,'keywords'=>$keywords,'description'=>$description);
} else {
$result = array('code'=>-1,'msg'=>'站点信息获取失败!');
}
if (json_encode($result) === false) {
return json(['code' => -1, 'msg' => '站点信息获取失败!']);
} else {
return json($result);
}
更多推荐内容
- AudioRelay 0.27.5 手机充当电脑音响 9 天前
- AIGC指令进阶课:AI思维培养与高效生活应用 11 天前
- DeepSeek全栈开发实战:AI应用落地全链路指南 11 天前
- 花生十三 2025网课资料全套合集 11 天前
- 网易云音乐小灯泡抽SVIP会员 12 天前
- Google外贸客户开发实战是一本非常实用的快速入门指南。 16 天前
- 星辰八字教程:基础到进阶全解析 19 天前
- AI办公与副业创收实战指南 19 天前
- 正统道学精要:百年集成50册 19 天前
- 配方技术分享-粽子配方多套 20 天前

文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系客服并出示版权证明以便删除!
安卓度豆v2.3.0.10 你的私人女友多样交互随心所欲 完全免费
« 上一篇
03-20
孜然SEO单页引流系统源码V1.0【自动裂变URL】
下一篇 »
03-20