每天上下班都要盯着公交车的动态,很难受,所以小杰想做一个自动化获取车辆信息,并且车子快到了就及时提醒我的jio本,在忙碌几个小时之后成功提取到了车辆信息,但是自动化的工作还需要各位自己去弄一下,小杰只分享获取车辆信息的接口。
<?php /** * 车来了APP公交车信息 * @author Youngxj * @email blog@youngxj.cn * @blogurl blog.youngxj.cn * @time 2019年7月16日 */ class chelaile { // 城市信息接口 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 = ''; //城市ID private $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) /** * 获取城市信息 * @Author Youngxj * @DateTime 2019-07-11 * @return [type] [description] */ public function getCity() { $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 { return false; } } else { return false; } } /** * 获取附近站点的线路及信息 * @Author Youngxj * @DateTime 2019-07-11 * @return [type] [description] */ public function getArr() { $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 { return false; } } else { return false; } } /** * 获取公交车实时信息 * @Author Youngxj * @DateTime 2019-07-11 * @param [type] $lineId 线路lineId * @param [type] $geo_lng 公交站点lng * @param [type] $geo_lat 公交站点lat * @return [type] [description] */ public function getBusInfo() { $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 { return false; } } else { return false; } } /** * 获取公交车细节 * @Author Youngxj * @DateTime 2019-07-11 * @param [type] $targetOrder 所在公交站点ID * @return [type] [description] */ public function getBusesDetail($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 { return false; } } else { return false; } } /** * 获取公交时刻表 * @Author Youngxj * @DateTime 2019-07-11 * @param [type] $sId 所在公交站的sid * @return [type] [description] */ public function getBusTime($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 { return false; } } else { return false; } } /** * curl模拟提交 * @param [type] $url 访问的URL * @param string $post post数据(不填则为GET) * @param string $referer 自定义来路 * @param string $cookie 提交的$cookies * @param integer $returnCookie 是否返回$cookies * @param string $ua 自定义UA * @return [type] [description] */public function curl_request($url, $post = '', $referer = '', $cookie = '', $returnCookie = 0, $ua = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/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)) { return curl_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码) public function addressbaidu($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 { return false; } } #获取高德经纬度(key=高德key值) public function addresstolatlag($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'])) { return explode(',', $result['geocodes']['0']['location']); } else { return false; } } } /** * 方糖通知 * @Author Youngxj * @DateTime 2019-07-12 * @param [type] $text 标题 * @param string $desp 内容 * @param string $key [description] * @return [type] [description] */ public function sc_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 = new chelaile(); // 获取百度地图经纬度 $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%AF urldecode编码 (路) $chelaile->lineId = '0574-628%E8%B7%AF-' . $fx; if ($xj) { // 获取公交细节信息 $getBusesDetail = $chelaile->getBusesDetail($zd); //echo json_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(); //echo json_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是否能正确的获取到。
API
2022-06-15 121API是开发中必备知识点,合理的应用API可以大大节省开发成本,为您整理收集了包含API接口的信息,包含API接口,API代码,API源码,助你更快的学习使用API技术....
- 带五套模板的API接口系统源码 [2024-01-18]
- layui框架API接口调用管理系统源码 [2024-01-16]
- 小西瓜API管理系统源码2.0 [2023-12-07]
- ApiDebug在线api接口调试浏览器插件 [2023-12-07]
- 抖音短视频去水印官方API接口源码 [2023-10-04]