PHP火車餘票查詢的API,12306官方的API

來源:互聯網
上載者:User

我在做自己微信的時候需要的火車票查詢的東西,註冊的東西等等太多了,今天就簡單的做了下測試看能擷取到不,資料怎麼樣處理速度怎麼樣,因為是個人做所以就沒有去其他網站上找介面,就自己做了下!下邊把方法放出來!

 

<?php/** *火車票查詢方法 簡單嘗試 * * @copyrightwiduu * @licensehttp://www.widuu.com * @lastmodify2013-6-20 */function json_array($json){if($json){foreach ((array)$json as $k=>$v){$data[$k] = !is_string($v)? json_array($v):$v;}return $data;}}function doget ($start,$end,$time) // get擷取資料使用{if(empty($time)){$time = date('Y-m-d',time());}else{if(substr($time,0,1)!=0){$time = date('Y-0',time()).$time;echo substr($time,0,1);}else{$time = date('Y-',time()).$time;}}$name = include ("../name.php");$star = $name[$start];$end = $name[$end];$url = "http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryLeftTicket&orderRequest.train_date={$time}&orderRequest.from_station_telecode={$star}&orderRequest.to_station_telecode={$end}&orderRequest.train_no=&trainPassType=QB&trainClass=QB%23D%23Z%23T%23K%23QT%23&includeStudent=00&seatTypeAndNum=&orderRequest.start_time_str=00%3A00--24%3A00";$optionget = array('http' => array('method' => "GET", 'header' => "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.0.04506; CIBA)\r\nAccept:*/*\r\nReferer:http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=init")); $file = file_get_contents($url, false , stream_context_create($optionget));return json_array(json_decode($file));}$result = doget("北京","廊坊","6-23");  $result = strip_tags($result['datas']);$return_str = str_replace(" ","",$result);$return_str = str_replace("\\n","",$return_str);$a = explode(",",$return_str);$name =array();$c = array_chunk($a,16);array_pop($c);foreach($c as $k =>$v){$str="餘票:<br>商務座:".$v[5].",特等座:".$v[6].",一等座:".$v[7].",二等座:".$v[8].",進階軟臥:".$v[9].",軟臥:".$v[10].",硬臥:".$v[11].",軟座:".$v[12].",硬座:".$v[13].",無座:".$v[14].",其他:".$v[15];$str = preg_replace("/,硬座\:--,/","",$str);$str = preg_replace("/商務座\:--,/","",$str);$str = preg_replace("/,特等座\:--,/","",$str);$str = preg_replace("/,一等座\:--,/","",$str);$str = preg_replace("/,二等座\:--,/","",$str);$str = preg_replace("/,進階軟臥\:--,/","",$str);$str = preg_replace("/,軟臥\:--,/","",$str);$str = preg_replace("/,硬臥\:--,/","",$str);$str = preg_replace("/,軟座\:--,/","",$str);$str = preg_replace("/,硬座\:--,/","",$str);$str = preg_replace("/,其他\:--/","",$str);echo "車次:{$v[1]},發站:{$v[2]},到站:{$v[3]},曆時:{$v[4]},<br>{$str}<br>";}http://www.widuu.com/archives/06/682.html


相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。