自建 PHP获取网站描述Description API接口

最近在开发一款AIGC网址导航,带有网址投稿功能,因为后台添加导航比较繁琐(虽然只有几个字段),我也常常用前台的投稿功能然后再去后台审核下。

提交的网址涉及几个常见字段,titile、描述、URL、icon、分类… 好像搞成自动的也不是很难,所以写了两个接口,一个获取网站描述,一个获取网址title。

本文介绍获取网站描述。title请看:自建 PHP获取网站标题Title API接口

首先获取逻辑

  1. 优先获取网站的description标签,如果这个标签没有内容或不存在,就用h2标签,如果还是没有就自动获取网站中的内容简述。 api访问格式为https://api.xxx.cn/desc/get.php?url=网址
  2. 运营干久了,没有日志日后怎么分析呢。
  3. 添加调用日志的功能。保存180天,怕被打成超大日志文件影响速度,分拆成最大占用文件2M。如果超过2M就新建一个日志来写。日志包括调用者的调用时间、ip、ua设备、调用结果等信息
  4. 还有一个判断数据 是否调用成功,并输出到日志中。如果调用成功1分钟内不允许调用。简单防止下被刷(由于跨域调用问题,这个冲突就注释掉了)
  5. 解决跨域调用问题
  6. 集成到AI网址导航提交页面
  值得一提的是,以上功能均借助AI帮忙完成,所有功能前前后后20多分钟就写好了,借助AI,写代码直接提升一个level。感兴趣的可以试试: AI来帮忙|多功能AI助手

效果

%title插图%num%title插图%num

 

本站接口&代码

集成到前端 代码参考

<div class="zmki_navs_form-row">
                <label for="post_custom_url">URL:</label>
                <div class="form-field">
                    <input type="url" name="post_custom_url" id="post_custom_url" placeholder="请以https:// 或 http://开头">
                    <div class="zmki_navs_menu_tiqu">
                        <button id="auto_extract_button">自动提取标题描述</button>
                    </div>
                </div>
            </div>

<style>
.zmki_navs_menu_tiqu{
    margin-top: -19px;
    margin-bottom: 10px;
    margin-left: 1px;
    border-radius: 4px;
}
.zmki_navs_menu_tiqu button{
    border: 0px;
    background: #e4e4e46e;
    color: #4d4d4d87;
}
  
</style>
<!--提取标题-->
<script>
document.getElementById("auto_extract_button").addEventListener("click", function() {
    var url = document.getElementById("post_custom_url").value;
    if (url) {
        var apiUrl = "https://api.xxx.cn/title/get.php?url=" + encodeURIComponent(url);
        
        fetch(apiUrl)
            .then(response => response.json())
            .then(data => {
                if (data.title) {
                    document.getElementById("post_title").value = data.title;
                } else {
                    alert("获取描述失败,请手动输入");
                }
            })
            .catch(error => {
                console.log(error);
                alert("获取描述失败,请手动输入");
            });
    } else {
        alert("请输入有效的URL");
    }
});
</script>
<!--提取描述-->
<script>
document.getElementById("auto_extract_button").addEventListener("click", function() {
    var url = document.getElementById("post_custom_url").value;
    if (url) {
        var apiUrl = "https://api.xxx.cn/desc/get.php?url=" + encodeURIComponent(url);
        
        fetch(apiUrl)
            .then(response => response.json())
            .then(data => {
                if (data.description) {
                    document.getElementById("post_description").value = data.description;
                } else {
                    alert("获取描述失败,请手动输入");
                }
            })
            .catch(error => {
                console.log(error);
                alert("获取描述失败,请手动输入");
            });
    } else {
        alert("请输入有效的URL");
    }
});
</script>

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容