菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

使用PHP获取公交车信息的API

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:php教程 API接口 php获取

PHP车来了API是一个用于获取公交车信息的接口。通过这个API,我们可以获取到公交车的实时位置、预计到达时间、线路信息等数据。要使用这个API,首先需要在官网注册一个账号并获取API密钥。然后,可以使用PHP的cURL库来发起HTTP请求,将API密钥和其他参数作为请求参数发送给服务器。服务器会返回一个JSON格式的数据,我们可以通过解析这个数据来获取所需的公交车信息。在实际应用中,可以将这个API与地图服务结合,为用户提供实时的公交车位置和路线规划功能。

每天上下班都要盯着公交车的动态,很难受,所以小杰想做一个自动化获取车辆信息,并且车子快到了就及时提醒我的jio本,在忙碌几个小时之后成功提取到了车辆信息,但是自动化的工作还需要各位自己去弄一下,小杰只分享获取车辆信息的接口。

<?php/***车来了APP公交车信息*@authorYoungxj*@emailblog@youngxj.cn*@blogurlblog.youngxj.cn*@time2019年7月16日*/classchelaile{//城市信息接口private$citylist_url='https://web.chelaile.net.cn/cdatasource/citylist';//附近站点线路及信息private$homePageInfo='https://api.chelaile.net.cn/bus/stop!homePageInfo.action';//实时接口private$lineDetail='http://api.chelaile.net.cn/bus/line!lineDetail.action';//公交车细节private$busesDetail='http://api.chelaile.net.cn/bus/line!busesDetail.action';//公交车时刻表private$getBusTime='http://api.chelaile.net.cn/bus/line!depTimeTable.action';public$lat='';//维度public$lng='';//经度private$gpstype='wgs';private$s='android';//(必须,否则非法授权访问)private$v='3.80.0';//(必须,否则非法授权访问)private$src='webapp_default';private$userId='';public$sign='PPgBLFF5779koWJpY09iQg%3D%3D';//签名(无签名非法授权访问)public$lineId='';//(0574-628路-0)public$cityId='';//城市IDprivate$apiUrl2='https://api.chelaile.net.cn/bus/stop!homePageInfo.action?type=1&act=2&gpstype=wgs&gpsAccuracy=65.000000&cityId=城市ID(从前面一个接口获取)&hist=&s=IOS&sign=&dpi=3&push_open=1&v=5.50.4&lat=纬度&lng=经度';private$geo_type='';//(必备gcj)/***获取城市信息*@AuthorYoungxj*@DateTime2019-07-11*@return[type][description]*/publicfunctiongetCity(){$url=$this->citylist_url.'?type=gpsRealtimeCity&lat='.$this->lat.'&lng='.$this->lng.'&gpstype='.$this->gpstype.'&s='.$this->s.'&v='.$this->v.'&src='.$this->src.'&userId='.$this->userId;$data=$this->curl_request($url);if($json_data=json_decode($data,1)){if($json_data&&isset($json_data['status'])&&$json_data['status']=='OK'){return$json_data['data']['gpsRealtimeCity'];}else{returnfalse;}}else{returnfalse;}}/***获取附近站点的线路及信息*@AuthorYoungxj*@DateTime2019-07-11*@return[type][description]*/publicfunctiongetArr(){$url=$this->homePageInfo.'?type=1&act=2&gpstype='.$this->gpstype.'&gpsAccuracy=65.000000&cityId='.$this->cityId.'&hist=&s='.$this->s.'&sign=&dpi=3&push_open=1&v=5.50.4&lat='.$this->lat.'&lng='.$this->lng;$data_json=$this->curl_request($url);$data=str_replace(array("YGKJ##",'**YGKJ'),"",$data_json);if($json_data=json_decode($data,1)){if($json_data&&isset($json_data['jsonr']['status'])&&$json_data['jsonr']['status']=='00'){return$json_data['jsonr']['data'];}else{returnfalse;}}else{returnfalse;}}/***获取公交车实时信息*@AuthorYoungxj*@DateTime2019-07-11*@param[type]$lineId线路lineId*@param[type]$geo_lng公交站点lng*@param[type]$geo_lat公交站点lat*@return[type][description]*/publicfunctiongetBusInfo(){$url=$this->lineDetail.'?sign='.$this->sign.'&cityId='.$this->cityId.'&geo_type=gcj&lineId='.$this->lineId.'&isNewLineDetail=1&s='.$this->s.'&last_src=app_xiaomi_store&geo_lng='.$this->lng.'&geo_lat='.$this->lat.'&v='.$this->v;$data_json=$this->curl_request($url);$data=str_replace(array("YGKJ##",'**YGKJ'),"",$data_json);if($json_data=json_decode($data,1)){if($json_data&&isset($json_data['jsonr']['status'])&&$json_data['jsonr']['status']=='00'){return$json_data['jsonr']['data'];}else{returnfalse;}}else{returnfalse;}}/***获取公交车细节*@AuthorYoungxj*@DateTime2019-07-11*@param[type]$targetOrder所在公交站点ID*@return[type][description]*/publicfunctiongetBusesDetail($targetOrder){$url=$this->busesDetail.'?sign='.$this->sign.'&cityId='.$this->cityId.'&lat='.$this->lat.'&geo_type=gcj&gpstype=gcj&geo_lt=5&screenDensity=3.0&flpolicy=1&stats_referer=searchHistory&targetOrder='.$targetOrder.'&lineId='.$this->lineId.'&s='.$this->s.'&geo_lng='.$this->lng.'&geo_lat='.$this->lat.'&v='.$this->v;$data_json=$this->curl_request($url);$data=str_replace(array("YGKJ##",'**YGKJ'),"",$data_json);if($json_data=json_decode($data,1)){if($json_data&&isset($json_data['jsonr']['status'])&&$json_data['jsonr']['status']=='00'){return$json_data['jsonr']['data'];}else{returnfalse;}}else{returnfalse;}}/***获取公交时刻表*@AuthorYoungxj*@DateTime2019-07-11*@param[type]$sId所在公交站的sid*@return[type][description]*/publicfunctiongetBusTime($sId='0574-6437'){$url=$this->getBusTime.'?sign='.$this->sign.'&cityId=&stationId='.$sId.'&geo_type=gcj&lineId='.$this->lineId.'&s=android&geo_lng='.$this->lng.'&geo_lat='.$this->lat.'&v='.$this->v;$data_json=$this->curl_request($url);$data=str_replace(array("YGKJ##",'**YGKJ'),"",$data_json);if($json_data=json_decode($data,1)){if($json_data&&isset($json_data['jsonr']['status'])&&$json_data['jsonr']['status']=='00'){return$json_data['jsonr']['data'];}else{returnfalse;}}else{returnfalse;}}/***curl模拟提交*@param[type]$url访问的URL*@paramstring$postpost数据(不填则为GET)*@paramstring$referer自定义来路*@paramstring$cookie提交的$cookies*@paraminteger$returnCookie是否返回$cookies*@paramstring$ua自定义UA*@return[type][description]*/publicfunctioncurl_request($url,$post='',$referer='',$cookie='',$returnCookie=0,$ua='Mozilla/5.0(WindowsNT6.1;WOW64;rv:43.0)Gecko/20100101Firefox/43.0'){$curl=curl_init();curl_setopt($curl,CURLOPT_URL,$url);curl_setopt($curl,CURLOPT_USERAGENT,$ua);curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);curl_setopt($curl,CURLOPT_AUTOREFERER,1);curl_setopt($curl,CURLOPT_TIMEOUT,60);curl_setopt($curl,CURLOPT_REFERER,$referer);curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);$httpheader[]="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";$httpheader[]="Accept-Encoding:gzip,deflate";$httpheader[]="Accept-Language:zh-CN,zh;q=0.9";$httpheader[]="Connection:close";curl_setopt($curl,CURLOPT_HTTPHEADER,$httpheader);curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);if($post){curl_setopt($curl,CURLOPT_POST,1);curl_setopt($curl,CURLOPT_POSTFIELDS,http_build_query($post));}if($cookie){curl_setopt($curl,CURLOPT_COOKIE,$cookie);}curl_setopt($curl,CURLOPT_HEADER,$returnCookie);curl_setopt($curl,CURLOPT_TIMEOUT,10);curl_setopt($curl,CURLOPT_ENCODING,"gzip");curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);$data=curl_exec($curl);if(curl_errno($curl)){returncurl_error($curl);}curl_close($curl);if($returnCookie){list($header,$body)=explode("rnrn",$data,2);preg_match_all("/Set-Cookie:([^;]*);/",$header,$matches);$info['cookie']=substr($matches[1][1],1);$info['content']=$body;return$info;}else{return$data;}}#获取百度经纬度(ak=百度ak码)publicfunctionaddressbaidu($address)//$address:地址{$url='http://api.map.baidu.com/geocoder/v2/?address='.$address.'&output=json&ak=Bsr5iefxHEwQD8iCFTx3GwWOem0ZoSBk';if($result=file_get_contents($url)){$arr=explode(',"lat":',substr($result,40,36));return$arr;}else{returnfalse;}}#获取高德经纬度(key=高德key值)publicfunctionaddresstolatlag($address){//$address:地址$url='http://restapi.amap.com/v3/geocode/geo?key=a5767d2101d83dffcb6cc0325eaccfb4&s=rsv3&city=35&address='.$address;if($result=file_get_contents($url)){$result=json_decode($result,true);//判断是否成功if(!empty($result['count'])){returnexplode(',',$result['geocodes']['0']['location']);}else{returnfalse;}}}/***方糖通知*@AuthorYoungxj*@DateTime2019-07-12*@param[type]$text标题*@paramstring$desp内容*@paramstring$key[description]*@return[type][description]*/publicfunctionsc_send($text,$desp='',$key=''){$postdata=http_build_query(array('text'=>$text,'desp'=>$desp,));$opts=array('http'=>array('method'=>'POST','header'=>'Content-type:application/x-www-form-urlencoded','content'=>$postdata,),);$context=stream_context_create($opts);return$result=file_get_contents('https://sc.ftqq.com/'.$key.'.send',false,$context);}}

使用方法

<?php$chelaile=newchelaile();//获取百度地图经纬度$localtion=$chelaile->addressbaidu('这里改成自己车站的站名');if(!$localtion){exit('地址定位获取失败');}//反向$fx=isset($_GET['fx'])?1:0;//细节$xj=isset($_GET['xj'])?1:0;//站点ID$zd=isset($_GET['zd'])?$_GET['zd']:'10';//签名$chelaile->sign='PPgBLFF5779koWJpY09iQg%3D%3D';//当前公交站定位if($fx){$chelaile->lng=isset($localtion[0])?$localtion[0]:'';$chelaile->lat=isset($localtion[1])?$localtion[1]:'';}else{$chelaile->lng=isset($localtion[0])?$localtion[0]:'';$chelaile->lat=isset($localtion[1])?$localtion[1]:'';}//获取城市信息$cityInfo=$chelaile->getCity();if($cityInfo&&isset($cityInfo['cityId'])&&$cityInfo['cityId']!=''){$chelaile->cityId=$cityInfo['cityId'];//$testInfo=$chelaile->getArr();//var_dump($testInfo);exit();//%E8%B7%AFurldecode编码(路)$chelaile->lineId='0574-628%E8%B7%AF-'.$fx;if($xj){//获取公交细节信息$getBusesDetail=$chelaile->getBusesDetail($zd);//echojson_encode($getBusesDetail);echo'当前站台:'.$getBusesDetail['targetOrder'].'<br/>';echo'线路状态:'.$getBusesDetail['line']['assistDesc'].','.$getBusesDetail['line']['desc'].'<br/>';if(isset($getBusesDetail['buses'][0])){echo'车牌号:'.$getBusesDetail['buses'][0]['busId'].'<br/>';echo'距离站点:'.$getBusesDetail['buses'][0]['distanceToSc'].'m<br/>';}echo'车辆状态:'.$getBusesDetail['tip']['desc'].'<br/>';exit();}//获取公交实时信息$getBusInfo=$chelaile->getBusInfo();//echojson_encode($getBusInfo);echo'当前位置:'.$chelaile->lat.','.$chelaile->lng.'<br/>';echo'始发站:'.$getBusInfo['line']['startSn'].'<br/>';echo'终点站:'.$getBusInfo['line']['endSn'].'<br/>';echo'首班车:'.$getBusInfo['line']['firstTime'].'<br/>';echo'末班车:'.$getBusInfo['line']['lastTime'].'<br/>';echo'公交线:'.$getBusInfo['line']['name'].'<br/>';echo'每位票价:'.$getBusInfo['line']['price'].'<br/>';echo'车辆状态:'.$getBusInfo['line']['desc']?$getBusInfo['line']['desc'].'<br/>':'在路上……'.'<br/>';echo'车辆描述:'.$getBusInfo['depDesc']?$getBusInfo['depDesc'].'<br/>':'在路上……'.'<br/>';echo'提示:'.$getBusInfo['tip']['desc'].'<br/>';//if(isset($getBusInfo['tip']['desc'])){//$sytime=preg_match_all('/d/',$getBusInfo['tip']['desc'],$string);//var_dump($sytime);//if(is_numeric($sytime)){//$msg='公交线:'.$getBusInfo['line']['name'].'还有'.$sytime.'分钟到达';//$chelaile->sc_send('车来了',$msg);//}//}if(isset($getBusInfo['stations'])){echo'#############################以下是车站信息#####################################<br/>';foreach($getBusInfo['stations']as$key=>$value){echo'ID:'.$getBusInfo['stations'][$key]['order'].'站台名:'.$getBusInfo['stations'][$key]['sn'].'距离站点:'.$getBusInfo['stations'][$key]['distanceToSp'].'M{经度:'.$getBusInfo['stations'][$key]['lat'].'纬度:'.$getBusInfo['stations'][$key]['lng'].'}<br/>';}}}else{exit('获取城市ID失败');}

大部分信息都是需要自己改成自己的,比如经纬度,站点信息等等,还需要确定自己城市是否支持车来了APP以及cityId是否能正确的获取到。

相关专题
评论
建站知识
建站知识
使用技巧
调试安装
运营推广