全国潮汐表查询源码,基于Thinphp5.1框架开发,带全国所有港口的实时潮汐查询,自动采集潮汐数据,另带3种接口,避免接口数据无法使用,自动切换线路采集源,可查询全国港口最近15天内的潮汐表。
潮汐实时数据来源:中国海事局。
环境说明:
本源码内核是ThinkPHP5.1 ,在thinkcmf框架上显示,支持php7以上。推荐php7.2
php.ini打开扩展
extension=php_openssl.dll
TP默认模块修改
\data\config\app.php 'default_module' => 'chaoxi',
路由地址修改
\data\route\url.php
安装步骤
1.第一步:把 chaoxi.sql数据库还库到mysql里
2.修改数据链接配置文件:\data\config\database.php ,修改里的用户和密码 和数据库里
3.域名绑定到 /public/ 目录下
4. 直接访问域名就可以打开
效果图:
建立一个全国港口地区表,根据不现的地区代码查询接口数据,转存到自己数据库。
部分PHP源码:
class ChaoxiController extends HomeBaseController
{
public function index()
{
$province = [
"辽宁","河北","天津","山东",
"江苏","上海","浙江","福建","台湾","广东","香港","澳门",
"海南","广西","其他","三沙市"
];
$type = input('type','');
if($type=='')
{
$list = [];
foreach ($province as $k=>$v)
{
$city = Db::name('chaoxi')->where('province',$v)->group('city')->select()->toArray();
foreach ($city as $kk=>$vv)
{
$port = Db::name('chaoxi')->where('city',$vv['city'])->select()->toArray();
$city[$kk]['port'] = $port;
}
$list[] = [
'province'=>$v,
'city'=>$city
];
}
//print_r($list);die;
$last_date = date('Y-m-d',strtotime('-90 days'));
Db::name('chaoxi_json')->where('date','<=',$last_date)->delete();
$this->assign('list',$list);
return $this->fetch();
}
public function city()
{
$date = date('Y-m-d');
$day = input('day', $date);
$day_time = strtotime($day);
$weekarray=array("日","一","二","三","四","五","六");
//先定义一个数组
$day_week = "星期".$weekarray[date("w",$day_time)];
//未来15天
//先定义一个数组
$weekarray=array("日","一","二","三","四","五","六");
$day_now = date('Y-m-d');
$day15 = [];
$day15[] = [
'day'=> $day_now,
'week'=>"星期".$weekarray[date("w",strtotime($day_now))]
];
for ($i=1;$i<=15;$i++)
{
$day15[] = [
'day'=>date('Y-m-d', strtotime("+{$i} day")),
'week'=>"星期".$weekarray[date("w",strtotime("+{$i} day"))]
];
}
//print_r($day15);die;
$this->assign('day15',$day15);
$this->assign('day_str', date('Y年m月d日').' '.$day_week);
$this->assign('day', $day);
$city = input('city','');
$list = Db::name('chaoxi')->where('pinyin',$city)->select()->toArray();
$tide_list = [];
foreach ($list as &$v)
{
$tide = self::get_tide_portid($v['port_id'], $day);
$v['tide_time'] = json_encode($tide['tide_time']);
$v['tide'] = json_encode($tide['tide']);
}
//print_r($list);die;
$this->assign('list', $list);
$this->assign('data', $list[0]);
return $this->fetch();
}
//港口数据
public function port()
{
$date = date('Y-m-d');
$id = input('id','');
$day = input('day', $date);
$data = Db::name('chaoxi')->where('port_id',$id)->find();
$script = '';
$ports = Db::name('chaoxi')->where('city',$data['city'])->select();//同市时其他港口
if($data)
{
$time = time();
$find_json = Db::name('chaoxi_json')->where(['port_id'=>$data['port_id'],'date'=>$day])->order('id desc')->value('json');
$find_json_arr = json_decode($find_json, true);
//print_r($find_json_arr);die;
if(empty($find_json['script']))
{
$chaoxb = file_get_contents('https://www.xxxx.com/'.$id.'/?day='.$day);
//如果想只匹配一次,可以用preg_match函数
}
else
{
$script = $find_json['script'];
}
//print_r($find_json_arr);
$tide = '';
if(!empty($find_json_arr[0]['data']))
{
$tide = self::tide_json_data($find_json);
if(strpos($data['port_name'],'港')===false)
{
$data['port_name']=$data['port_name'].'港';
}
}
}
//print_r($tide);die;
$day_time = strtotime($day);
$weekarray=array("日","一","二","三","四","五","六");
//先定义一个数组
$day_week = "星期".$weekarray[date("w",$day_time)];
//未来15天
//先定义一个数组
$weekarray=array("日","一","二","三","四","五","六");
$day_now = date('Y-m-d');
$day15 = [];
$day15[] = [
'day'=> $day_now,
'week'=>"星期".$weekarray[date("w",strtotime($day_now))]
];
for ($i=1;$i<=15;$i++)
{
$day15[] = [
'day'=>date('Y-m-d', strtotime("+{$i} day")),
'week'=>"星期".$weekarray[date("w",strtotime("+{$i} day"))]
];
}
$this->assign('day15',$day15);
$this->assign('script', $script);
$this->assign('tide', $tide);
$this->assign('date', $date);
$this->assign('day_str', date('Y年m月d日').' '.$day_week);
$this->assign('data', $data);
$this->assign('day', $day);
$this->assign('ports', $ports);
return $this->fetch();
}
//根据港口和日期取潮汐数据并处理
private function get_tide_portid($port_id, $day='')
{
$day = empty($day) ? date('Y-m-d') : $day;
$find_json = Db::name('chaoxi_json')->where(['port_id'=>$port_id,'date'=>$day])->value('json');
$json_arr = json_decode($find_json,true);
$port = Db::name('chaoxi')->where(['port_id'=>$port_id])->find();
if(empty($json_arr[0]['data']))
{
$time = time();
// $url = "https://www.cnss.com.cn;
$url = "https://www.cnss.com/evip/".urlencode($port['port_name'])."_{$day}.json?v=$time";
$json = file_get_contents($url);
if(!empty($json))
{
$tideJson_arr = json_decode($json, true);
//采集不到数据
if(empty($tideJson_arr[0]['data']))
{
$chaoxb = file_get_contents('https://www.cnss.com/'.$port_id.'/?day='.$day);
}
else
{
$data_json['json'] = $json;
$data_json['port_id'] = $port_id;
$data_json['date'] = $day;
$data_json['add_time'] = date('Y-m-d H:i:s');
Db::name('chaoxi_json')->insert($data_json);
$find_json = $json;
}
}
}
return self::tide_json_data($find_json);
}
//同步采集数据
public function syn()
{
set_time_limit(0);//0表示没有限制
$list = Db::name('chaoxi')->limit(190, 10)->select()->toArray();
//print_r($list);die;
$today = date("Y-m-d");
$today_time = time();
$days_arr = [];
for($i=0;$i<15;$i++)
{
$day = date("Y-m-d",strtotime("+{$i} day"));
$days_arr[$i] = $day;
}
foreach ($list as $k=>$v)
{
foreach ($days_arr as $kk=>$vv)
{
self::get_tide_portid($v['port_id'], $vv);
}
}
echo 'success!'.date('H:i:s');
}
}
演示地址:https://www.aichaoxibiao.com/
具体代码实现源码:https://www.uihtm.com/thinkphp/8354.html