最近有点无聊,闲着使用PHP语言请求了一些常用的API接口,下面我就带大家看下具体实现方式
我这里使用了http请求类库
http请求类库–composer安装方式–
// 安装命令
composer require rmccue/requests
天气接口请求地址 : https://tianqiapi.com/free/day
根据IP查询当前城市天气状况
protected $appid = '你的APPID';
protected $appsecret = '你的密钥';
// 查询当前城市天气清空
public function test()
{
// 获取IP
$ip = $_SERVER["REMOTE_ADDR"];
// 接口地址
$url = "https://tianqiapi.com/free/day?ip=" . $ip . '&appid=' . $this->appid . '&appsecret=' . $this->appsecret;
// GET请求接口
$data = Requests::get($url);
// 接口数据解码
$tel_data = json_decode($data->body, true);
// 输出请求成功后的数据
return $this->success('ok', $tel_data);
}
效果图
翻译接口请求地址: https://api.vvhan.com/api/fy
翻译API接口
// 万能翻译
public function test1($info)
{
// 接口地址
$url = "https://api.vvhan.com/api/fy?text=" . $info;
// GET请求接口
$data = Requests::get($url);
// 接口数据解码
$tel_data = json_decode($data->body, true);
// 输出请求成功后的数据
return $this->success('ok', $tel_data);
}
效果图
- 多的接口就不一一进行演示了,大家可以去网上找一些自己喜欢接口进行亲测一下,以上接口我已亲测没问题,大家可以进行测试下哦。
评论区