用PHP搭建一个可以显示地址、ip、天气的头图
本文最后更新于 858 天前,其中的信息可能已经有所发展或是发生改变。

API

本文使用的是高德(缺德)开放平台的api,需使用的api有以下几种:

  1. IP定位-API文档-开发指南-Web服务 API|高德地图API
  2. 地理/逆地理编码-API文档-开发指南-Web服务 API|高德地图API
  3. 天气查询-API文档-开发指南-Web服务 API|高德地图API

(使用方法请查阅官网)

PHP

使用高德api构建一个自己的api

分析我们需要使用的数据

根据第一个api-ip定位可以得到以下有效信息: 国家、省份、城市、区县、经纬度

我们只需要国家、省份、城市、经纬度就可以了 (因为区县用流量的IP查询不准确,草)

第二个api-地理/逆地理编码就需要我们传入经纬度这个数据,就可以获取到adcode(区域编码,查询天气的关键)

第三个api-天气查询,是人都能看懂这是查询天气的api,需使用adcode,可以获取到一系列天气信息(国外无效)

构建新的api

<?php
header('content-type:application/json;charset=utf8');
function bilud_json($origin_str){
    $json_obj = json_decode($origin_str);
    $json_str = json_encode($json_obj, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
    return $json_str;
}
$user_ip=@$_REQUEST["ip"];//传入ip参数
$key='';//填写高德开放平台应用key
if($user_ip==null||!filter_var($user_ip, FILTER_VALIDATE_IP)){
    $user_ip = $_SERVER["REMOTE_ADDR"];
}
$ip_info=json_decode(bilud_json(file_get_contents('https://restapi.amap.com/v5/ip?key='.$key.'&type=4&ip='.$user_ip)));//获取第一个api信息
if($ip_info->country=='中国'){ //ip地域为中国时生成含有天气的json
    $city_info=json_decode(bilud_json(file_get_contents('https://restapi.amap.com/v3/geocode/regeo?key='.$key.'&location='.$ip_info->location)));
    $weather_info=json_decode(bilud_json(file_get_contents('https://restapi.amap.com/v3/weather/weatherInfo?key='.$key.'&city='.$city_info->regeocode->addressComponent->adcode.'&extensions=base')));
    $ar=array(
        'country'=>$ip_info->country,
        'province'=>$ip_info->province,
        'city'=>$ip_info->city,
        'weather'=>$weather_info->lives[0]->weather,
        'temperature'=>$weather_info->lives[0]->temperature,
        'winddirection'=>$weather_info->lives[0]->winddirection,
        'humidity'=>$weather_info->lives[0]->humidity,
        'reporttime'=>$weather_info->lives[0]->reporttime
    );//构建json
} else { //ip地域不为中国时生成不含有天气的json
        $ar=array(
        'country'=>$ip_info->country,
        'province'=>$ip_info->province,
        'city'=>$ip_info->city,
        'weather'=>null,
        'temperature'=>null,
        'winddirection'=>null,
        'humidity'=>null,
        'reporttime'=>null
    );//构建json
}
echo json_encode($ar, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);//输出
?>

生成图片

很简单 大概? 所以就不给大致思路了 (注:需在目录下创建'assets'和背景图片[JPG格式],并更名为'bg.jpg',再放一个自己想要的字体)

<?php
header('Content-type:image/jpeg');//设置mime type
function bilud_json($origin_str){
    $json_obj = json_decode($origin_str);
    $json_str = json_encode($json_obj, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
    return $json_str;
}

$domain='';//替换为自己的域名以及路径
$user_ip = $_SERVER["REMOTE_ADDR"]; //获取访客ip
$height=120; //文字第一行在图片上显示的位置
$info=json_decode(bilud_json(file_get_contents($domain.'/?ip='.$user_ip)));
$img = imagecreatefromjpeg('asstes/bg.jpg');
$black=ImageColorAllocate($img, 0, 0, 0);
$place=$info->country;
if($info->province!=''){
    $place=$place.'-'.$info->province;
}
if($info->city!=''){ 
    $place=$place.'-'.$info->city;
} //构建 国家-省-市 文本
imagettftext($img, 60, 0, 50, $height, $black, "asstes/mnjzy.ttf", "欢迎来自 ".$place." 的访客");
if(!($info->weather==null||$info->temperature==null||$info->winddirection==null||$info->humidity==null||$info->reporttime==null)){ //判断是否显示天气
    imagettftext($img, 50, 0, 18, $height+75, $black, "asstes/mnjzy.ttf", ' 当前天气 '.$info->weather.' '.$info->temperature.'℃ 风向 '.$info->winddirection.' 空气湿度 '.$info->humidity.'%');
    imagettftext($img, 30, 0, 50, $height+115, $black, "asstes/mnjzy.ttf", '更新于 '.$info->reporttime);
    imagettftext($img, 30, 0, 50, $height+155, $black, "asstes/mnjzy.ttf", '您的IP '.$user_ip);
} else {
    imagettftext($img, 30, 0, 50, $height+205, $black, "asstes/mnjzy.ttf", '您的IP '.$user_ip);
}//使用时请将"asstes/mnjzy.ttf"更改为自己的字体链接
imagejpeg($img);
imagedestroy($img);
?>

最后

就可以尽情使用了

这里提供一下我的api: 天气json api 头图

PHP打包文件: https://www.aliyundrive.com/s/J6NC6JzhJ7f(weather-271.exe)

版权信息: 本文采用 知识共享许可协议 CC BY-NC-SA 4.0 许可协议。转载和引用时请注意遵守协议、注明出处!
出处: https://loliurl.club/271.html
作者: heartalborada

评论

  1. Liu
    Windows Edge 91.0.864.67
    3 年前
    2021-7-12 19:14:04

    好!nb!

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Christmas
Character
Paimon's Paintings
上一篇
下一篇