记录我常用的免费API接口

目录

1.随机中英文句子

2.随机中英文句子(带图片和音频)

3.随机一句诗

4.随机一句话

5.随机一句情话

6. 随机一句舔狗语录

7.历史上的今天

8.获取来访者ip地址

9:获取手机号信息

10. 垃圾分类查询

11.字典查询

12.QQ信息查询


1.随机中英文句子

 API地址:https://api.kekc.cn/api/yien

Axios的get请求调用方式

  methods:{
        getit(){
        axios.get('https://api.kekc.cn/api/yien', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data.cn);
            this.cn = response.data.cn;
            this.en = response.data.en;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

获取结果示例:


2.随机中英文句子(带图片和音频)

API地址:https://api.oioweb.cn/api/common/OneDayEnglish

 Axios的get请求调用方式

  methods:{
        getit(){
        axios.get('https://api.oioweb.cn/api/common/OneDayEnglish', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.cn = response.data.result.note;
            this.en = response.data.result.content;
            this.imgurl = response.data.result.img;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

获取结果示例:


3.随机一句诗

API地址:https://v1.jinrishici.com/rensheng.txt

Axios的get请求调用方式

  methods:{
        getit(){
        axios.get('https://v1.jinrishici.com/rensheng.txt', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.cn = response.data;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

获取结果示例:


4.随机一句话

API地址:

https://api.kekc.cn/api/yiyan

https://api.kekc.cn/api/wawr

Axios的get请求调用方式

  methods:{
        getit(){
        axios.get('https://api.kekc.cn/api/yiyan', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.cn = response.data;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

获取结果示例:


5.随机一句情话

API地址:https://api.uomg.com/api/rand.qinghua

Axios的get请求调用方式

  methods:{
        getit(){
        axios.get('https://api.uomg.com/api/rand.qinghua', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.cn = response.data.content;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

获取结果示例:


6. 随机一句舔狗语录

API地址:http://api.kekc.cn/api/tiangou

Axios的get请求调用方式

  methods:{
        getit(){
        axios.get('http://api.kekc.cn/api/tiangou', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.cn = response.data;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

获取结果示例:


7.历史上的今天

API地址:https://api.oioweb.cn/api/common/history

Axios的get请求调用方式(需要注意的是会生成数组,此处仅使用了第一个元素)

  methods:{
        getit(){
        axios.get('https://api.oioweb.cn/api/common/history', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.year = response.data.result[0].year;
            this.title = response.data.result[0].title;
            this.link = response.data.result[0].link;
            this.desc = response.data.result[0].desc;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

 获取结果示例:


8.获取来访者ip地址

API地址:https://api.kekc.cn/api/getip

Axios的get请求调用方式

  methods:{
        getit(){
        axios.get('http://api.kekc.cn/api/getip', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.ipadd = response.data;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

获取结果示例:

 


9:获取手机号信息

API地址:https://api.oioweb.cn/api/common/teladress?mobile=10086

Axios的get请求调用方式(需要注意的是该api需要后缀11位手机号作为参数)

  methods:{
        getit(){
        axios.get('https://api.oioweb.cn/api/common/teladress?mobile=15990000000', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.prov = response.data.result.prov;
            this.city = response.data.result.city;
            this.name = response.data.result.name;
            this.postcode = response.data.result.postCode;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

获取结果示例:


10. 垃圾分类查询

API地址:https://api.oioweb.cn/api/common/rubbish?name=香蕉

Axios的get请求调用方式(需要注意的是该api需要后缀要查询的垃圾名称作为参数)

  methods:{
        getit(){
        axios.get('https://api.oioweb.cn/api/common/rubbish?name=香蕉', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.contain = response.data.result[0].contain;
            this.explain = response.data.result[0].explain;
            this.name = response.data.result[0].name;
            this.tip = response.data.result[0].tip;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

 获取结果示例:


11.字典查询

API地址:https://api.oioweb.cn/api/txt/dict?text=棒

 Axios的get请求调用方式(需要注意的是该api需要后缀要查询的字作为参数)

  methods:{
        getit(){
        axios.get('https://api.oioweb.cn/api/txt/dict?text=棒', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.pinyin = response.data.result.pinyin;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

需要注意的是get到的结果中包含了基础释义和详细释义,可视情况获取,本例中只取用拼音

 获取结果示例:


12.QQ信息查询

APD地址:https://api.oioweb.cn/api/qq/info?qq=12345

 Axios的get请求调用方式(需要注意的是该api需要后缀要查询的QQ号作为参数)

  methods:{
        getit(){
        axios.get('https://api.oioweb.cn/api/qq/info?qq=12345', {
            params: {
                // 请求参数
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.age = response.data.result.age;
            this.nickname = response.data.result.nickname;
            this.sex = response.data.result.sex;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

 获取结果示例:


补充:

在需要参数的API中可以使用params传递参数,例如12中可以在页面中添加输入框,再在js中拿到输入框的值并传递给axios请求地址中获取结果:

    <input type="text" id="qqInput">
    <button @click="getit()">发送请求</button>


      methods:{
        getit(){
        var qq = document.getElementById('qqInput').value;
        axios.get('https://api.oioweb.cn/api/qq/info', {
            params: {
                // 请求参数
                qq:qq
                }
            })
            .then( (response)=> {
            // 请求成功回调函数
            console.log(response.data);
            this.age = response.data.result.age;
            this.nickname = response.data.result.nickname;
            this.sex = response.data.result.sex;
            })
            .catch(function (error) {
            //请求失败回调函数
            console.log(error);
        });
    },
  }

 获取结果示例:


未完待续... (侵删)

<think>好的,用户想在PHP项目中添加随机文案功能。我需要先理解他的需求。随机文案通常是指每次页面加载时显示不同的文本内容,比如名言、提示语或者广告语等。 首先,我需要确定如何存储这些文案。常见的方法是用数组存储,或者从数据库、文件中读取。考虑到用户可能希望简单实现,使用数组可能是最直接的方式。不过,如果文案数量大,可能要考虑数据库。但用户没有提到数据量,所以先假设使用数组。 接下来,如何随机选择一条文案。PHP有array_rand函数,可以随机返回数组的键名。或者用shuffle打乱数组后取第一个元素。这两种方法都可以,但array_rand可能更高效,尤其是数组很大时。 然后,用户可能需要示例代码。我需要提供一个简单的例子,展示如何定义数组,随机选择并输出。同时,可能还需要考虑如何将代码嵌入到网页中,比如在HTML的某个位置用PHP输出。 另外,用户可能有扩展需求,比如从数据库读取。所以应该提到如何扩展,比如使用MySQL查询,并给出一个简单的示例。但需要提醒用户注意数据库连接和错误处理。 还要注意安全性,如果文案来自用户输入或外部源,需要转义输出,防止XSS攻击。使用htmlspecialchars函数是个好习惯,即使现在文案是静态的,也可能未来变化,所以提前建议比较好。 另外,性能方面,如果文案很多,每次读取文件或查询数据库可能影响性能,可以建议缓存机制,比如将文案数组缓存到内存中,或者使用APC、Redis等。但用户的问题可能不需要这么复杂,所以作为优化建议提到即可。 最后,总结步骤:准备文案数组,随机选择,输出到页面。提供两种方式的代码示例,并给出扩展方法和注意事项。这样用户可以根据自己的需求选择适合的方法。</think>在PHP中实现随机文案功能可通过以下步骤完成,提供两种实现方式: ### 一、基础数组实现(适合小型网站) ```php <?php // 定义文案数组 $copywriting = [ "每日进步1%,一年强大37倍", "代码是最好的文档", "优秀不是一种行为,而是一种习惯", "简单是可靠的先决条件" ]; // 随机选择一条 $randomText = $copywriting[array_rand($copywriting)]; // 输出到页面(注意转义防XSS) echo htmlspecialchars($randomText, ENT_QUOTES, 'UTF-8'); ?> ``` ### 二、数据库实现(适合内容较多的场景) ```php <?php // 数据库配置 $host = 'localhost'; $dbname = 'your_database'; $user = 'username'; $pass = 'password'; try { $pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $stmt = $pdo->query("SELECT content FROM copywriting ORDER BY RAND() LIMIT 1"); $randomText = $stmt->fetchColumn(); echo htmlspecialchars($randomText, ENT_QUOTES, 'UTF-8'); } catch(PDOException $e) { error_log("数据库连接失败: " . $e->getMessage()); echo "系统维护中,请稍后再访问"; } ?> ``` ### 架构建议 1. **数据存储层**:小型项目可直接使用数组,中大型建议使用Redis或MySQL 2. **性能优化**:频繁访问时推荐使用Redis缓存,其`SRANDMEMBER`命令可高效获取随机元素 3. **维护扩展**:建议建立后台管理界面方便文案维护[^1] ### 安全注意事项 1. 使用`htmlspecialchars()`防止XSS攻击 2. 数据库实现时使用预处理语句防SQL注入 3. 文件存储时需验证文件路径防止目录遍历
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值