標籤:
<?phpheader("content-type:text/html;charset=‘utf-8‘");//設定編碼echo "<meta content=‘text/html; charset=utf-8‘ http-equiv=‘Content-Type‘>"; //擷取API傳過來的資訊 queryDate 時間 from_station 始發站編號 to_station 到達站編號$url = "https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2016-09-31&from_station=GZQ&to_station=GAZ"; //擷取內容 內容是json格式$data = getContent($url);function getContent($url){//擷取網頁中輸出部分,並返回字串 $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $dxycontent = curl_exec($ch); curl_close($ch); return $dxycontent;}//轉碼$data = json_decode($data); //json轉碼之後是對象, 需要將對象轉數組 不然無法進行迴圈function object_array($array){ if(is_object($array)){ $array = (array)$array; } if(is_array($array)){ foreach($array as $key=>$value){ $array[$key] = object_array($value); } } return $array;}//得到數組資料$data2 = object_array($data);//稍稍美化一下echo ‘<table border="1">‘;echo ‘<tr><td>車次</td><td>始發站/到達站/終點站</td><td>出發時間/到達時間</td><td>曆時</td><td>商務座</td><td>特等座</td> <td>一等座</td><td>二等座</td><td>進階軟臥</td><td>軟臥</td><td>硬臥</td><td>軟座</td><td>硬座</td> <td>無座</td><td>其他</td><td>備忘</td></tr>‘;//迴圈foreach($data2[‘data‘][‘datas‘] as $k=>$v){ echo ‘<tr><td>‘.$v[‘station_train_code‘].‘</td><td>‘.$v[‘start_station_name‘].‘->‘.$v[‘to_station_name‘].‘->‘.$v[‘end_station_name‘].‘</td><td>‘.$v[‘start_time‘].‘ / ‘.$v[‘arrive_time‘].‘</td><td>‘.$v[‘lishi‘].‘</td><td>‘.$v[‘swz_num‘].‘</td><td>‘.$v[‘tz_num‘].‘</td><td>‘.$v[‘zy_num‘].‘</td><td>‘.$v[‘ze_num‘].‘</td><td>‘.$v[‘gr_num‘].‘</td><td>‘.$v[‘rw_num‘].‘</td><td>‘.$v[‘yw_num‘].‘</td><td>‘.$v[‘rz_num‘].‘</td><td>‘.$v[‘yz_num‘].‘</td><td>‘.$v[‘wz_num‘].‘</td><td>‘.$v[‘qt_num‘].‘</td><td>‘.$v[‘note‘].‘</td></tr>‘;}echo ‘</table>‘;/* * ["gr_num"]=>進階軟臥 * ["qt_num"]=>其他 * ["rw_num"]=> 軟臥 * ["rz_num"]=>軟座 * ["tz_num"]=>特等座 * ["wz_num"]=>無座 * ["yw_num"]=>硬臥 * ["yz_num"]=>硬座 * ["ze_num"]=>二等座 * ["zy_num"]=> 一等座 * ["swz_num"]=> 商務座 */
php 通過API介面串連12306餘票查詢