统计
  • 建站日期:2022-01-17
  • 文章总数:6255 篇
  • 评论总数:62630条
  • 分类总数:43 个
  • 最后更新:1天前

PHP获取网页标题和内容信息接口

作者头像
首页 综合教程 正文
广告

在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);
}

版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系客服并出示版权证明以便删除!
安卓无痛单词v2.10.0 解锁终身会员价值128元
« 上一篇 03-20
孜然SEO单页引流系统源码V1.0【自动裂变URL】
下一篇 » 03-20