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);
}
本站资源均为作者提供和网友推荐收集整理而来,仅供学习和研究使用,请在下载后24小时内删除,谢谢合作!
PHP获取网页标题和内容信息接口|不死鸟资源网
PHP获取网页标题和内容信息接口
此内容为免费阅读,请登录后查看
¥0
限时特惠
¥199
文章采用CC BY-NC-SA 4.0许可协议授权
免费阅读
THE END
点赞7 分享