获取网站名称、关键词、关键句

这段代码的作用是获取指定 URL 页面的 title、keywords 和 description,并以 JSON 格式返回给调用方。

<?php
// 获取 GET 请求参数中的 URL,如果没有则默认为 https://blogl.cn
$url = $_GET['url'] ?? 'https://blogl.cn';
$json['code'] = 1;

// 获取页面的 meta 标签信息
$meta_array = get_meta_tags($url);
$json['keywords'] = $meta_array['keywords'];
$json['description'] = $meta_array['description'];

// 获取页面的标题信息
$content = file_get_contents($url);
$way = '|<title>(.*)</title>|isU';
preg_match($way, $content, $matchs);
$json['title'] = $matchs[1];

// 返回 JSON 格式数据,并设置响应头为 UTF-8 编码
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
?>

效果如下:

使用方法:

http(s)://域名/文件.php?url=https://blogl.cn

这里是查询博客佬的网站名称、关键词、关键句信息。

阅读剩余
THE END