Skip to content

yuzechen45/-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

一言api

一个基于php的简单一言api

usage

把文件上传到服务器,访问install.php,完成安装 admin.php:管理后台 api.php:访问api config.php:数据库相关配置 目前仅支持mysql API 基础信息

· 基础URL: http://your-domain.com/api.php · 支持操作: · get - 获取随机一言 · count - 获取一言总数 · 支持格式: · json (默认) · text · html · 编码选项: · unicode (默认) · plain (URL编码)

  1. 直接浏览器访问

最简单的使用方式是在浏览器地址栏直接访问:

http://your-domain.com/api.php
http://your-domain.com/api.php?action=get&format=text
http://your-domain.com/api.php?action=count&format=json
  1. HTML/JavaScript 中使用

2.1 使用 Fetch API (现代浏览器)

// 获取随机一言 (JSON格式)
fetch('http://your-domain.com/api.php')
  .then(response => response.json())
  .then(data => {
    console.log(data.data); // 显示一言内容
    document.getElementById('hitokoto').innerText = data.data;
  })
  .catch(error => console.error('Error:', error));

// 获取随机一言 (文本格式)
fetch('http://your-domain.com/api.php?format=text')
  .then(response => response.text())
  .then(data => {
    console.log(data); // 显示一言内容
    document.getElementById('hitokoto').innerText = data;
  });

// 获取一言总数
fetch('http://your-domain.com/api.php?action=count')
  .then(response => response.json())
  .then(data => {
    console.log('总条数:', data.data);
    document.getElementById('count').innerText = `总共有 ${data.data} 条一言`;
  });

2.2 使用 XMLHttpRequest (兼容旧浏览器)

// 获取随机一言
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://your-domain.com/api.php', true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var data = JSON.parse(xhr.responseText);
    document.getElementById('hitokoto').innerText = data.data;
  }
};
xhr.send();

2.3 使用 jQuery

// 获取随机一言 (JSON格式)
$.getJSON('http://your-domain.com/api.php', function(data) {
  $('#hitokoto').text(data.data);
});

// 获取随机一言 (文本格式)
$.get('http://your-domain.com/api.php?format=text', function(data) {
  $('#hitokoto').text(data);
});

// 获取一言总数
$.getJSON('http://your-domain.com/api.php?action=count', function(data) {
  $('#count').text('总共有 ' + data.data + ' 条一言');
});

2.4 使用 Vue.js

<div id="app">
  <p>{{ hitokoto }}</p>
  <p>总共有 {{ count }} 条一言</p>
  <button @click="getHitokoto">获取一言</button>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script>
new Vue({
  el: '#app',
  data: {
    hitokoto: '加载中...',
    count: 0
  },
  mounted() {
    this.getHitokoto();
    this.getCount();
  },
  methods: {
    getHitokoto() {
      fetch('http://your-domain.com/api.php?format=text')
        .then(response => response.text())
        .then(data => {
          this.hitokoto = data;
        });
    },
    getCount() {
      fetch('http://your-domain.com/api.php?action=count')
        .then(response => response.json())
        .then(data => {
          this.count = data.data;
        });
    }
  }
});
</script>

2.5 使用 React

import React, { useState, useEffect } from 'react';

function Hitokoto() {
  const [hitokoto, setHitokoto] = useState('加载中...');
  const [count, setCount] = useState(0);

  useEffect(() => {
    // 获取一言
    fetch('http://your-domain.com/api.php?format=text')
      .then(response => response.text())
      .then(data => setHitokoto(data));
    
    // 获取总数
    fetch('http://your-domain.com/api.php?action=count')
      .then(response => response.json())
      .then(data => setCount(data.data));
  }, []);

  const refreshHitokoto = () => {
    fetch('http://your-domain.com/api.php?format=text')
      .then(response => response.text())
      .then(data => setHitokoto(data));
  };

  return (
    <div>
      <p>{hitokoto}</p>
      <p>总共有 {count} 条一言</p>
      <button onClick={refreshHitokoto}>换一句</button>
    </div>
  );
}

export default Hitokoto;
  1. 后端语言中使用

3.1 PHP

<?php
// 获取随机一言 (文本格式)
$hitokoto = file_get_contents('http://your-domain.com/api.php?format=text');
echo $hitokoto;

// 获取随机一言 (JSON格式)
$data = json_decode(file_get_contents('http://your-domain.com/api.php'), true);
echo $data['data'];

// 获取一言总数
$count_data = json_decode(file_get_contents('http://your-domain.com/api.php?action=count'), true);
echo "总共有 " . $count_data['data'] . " 条一言";

// 使用cURL获取
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://your-domain.com/api.php?format=text');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$hitokoto = curl_exec($ch);
curl_close($ch);
echo $hitokoto;
?>

3.2 Python

import requests

# 获取随机一言 (文本格式)
response = requests.get('http://your-domain.com/api.php?format=text')
hitokoto = response.text
print(hitokoto)

# 获取随机一言 (JSON格式)
response = requests.get('http://your-domain.com/api.php')
data = response.json()
print(data['data'])

# 获取一言总数
response = requests.get('http://your-domain.com/api.php?action=count')
data = response.json()
print(f"总共有 {data['data']} 条一言")

3.3 Node.js

const https = require('https');

// 获取随机一言 (文本格式)
https.get('http://your-domain.com/api.php?format=text', (resp) => {
  let data = '';
  resp.on('data', (chunk) => {
    data += chunk;
  });
  resp.on('end', () => {
    console.log(data);
  });
}).on('error', (err) => {
  console.log("Error: " + err.message);
});

// 使用axios库 (需要先安装: npm install axios)
const axios = require('axios');

axios.get('http://your-domain.com/api.php')
  .then(response => {
    console.log(response.data.data);
  })
  .catch(error => {
    console.error(error);
  });

3.4 Java

import java.net.*;
import java.io.*;

public class HitokotoAPI {
    public static void main(String[] args) {
        try {
            // 获取随机一言 (文本格式)
            URL url = new URL("http://your-domain.com/api.php?format=text");
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String hitokoto = reader.readLine();
            System.out.println(hitokoto);
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

About

一个基于php的简单一言api

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages