返回列表 发布新帖
查看: 27|回复: 0

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

1423

主题

0

回帖

4501

积分

论坛元老

积分
4501
发表于 2025-3-20 00:34:47 | 查看全部 |阅读模式
$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);
}
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright © 2001-2025 52线报网 版权所有 All Rights Reserved. |网站地图 闽ICP备19006036号-4
关灯 在本版发帖
扫一扫添加微信客服
返回顶部
快速回复 返回顶部 返回列表