用PHP搭建一个可以显示地址、ip、天气的头图

发布于 2021-07-12  1,967 次阅读


AI 摘要

本文介绍了如何使用 PHP 和高德开放平台的 API 搭建一个能够显示地址、IP 和天气信息的头图。首先列出了需要使用的几种 API,包括 IP 定位、地理/逆地理编码和天气查询。通过 IP 定位可以获取国家、省份、城市和经纬度信息,而地理/逆地理编码需要传入经纬度数据以获取区域编码。接着详细介绍了构建新 API 的步骤,包括获取用户 IP 地址、查询相关信息以及生成包含天气信息的 JSON 数据。最后讲述了如何生成图片,包括设置图片位置和文字内容,以及在目录下创建必要文件并替换域名路径的操作。

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);
?>


最后


就可以尽情使用了


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

间歇性发奋图强,持续性混吃等死
最后更新于 2024-05-04