php 通過API介面串連12306餘票查詢

來源:互聯網
上載者:User

標籤:

 

<?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餘票查詢

相關文章

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.