Php-based 12306 train ticket query interface call code example code description: php-based 12306 train ticket query interface call code example
// + ---------------------------------------------------------------------- // ------------------------------ // 12306 train ticket query call example code-aggregate data // online interface documentation: http://www.juhe.cn/docs/22//---------------------------------- Header ('content-type: text/html; charset = utf-8 '); // Configure Your applied appkey $ appkey = "*********************"; // ************* 1. station-to-station query (including fare) ************ $ url =" http://apis.juhe.cn/train/s 2 swithprice "; $ params = array (" start "=>" ", // departure station" end "=>" ", // terminal station" key "=> $ appkey, // application APPKEY (query on the application details page) "dtype" => "", // format of the returned data, xml or json, json by default ); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** ************ // ************** 2.12306 ticket booking ②: fare query ************* $ url =" http://apis.juhe.cn/train/ticket.price.php "; $ Params = array (" train_no "=>" ", // column number, corresponding to 12306 ticket booking ①: train_no" from_station_no "=> "", // departure station number, corresponding to 12306 booking ①: query the returned from_station_no "to_station_no" => "", // departure station number, corresponding to 12306 booking ①: query the returned to_station_no "date" => "", // The current day by default, in the format of 2014-12-25 "key" => $ appkey, // application APPKEY (query on the application details page); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring ); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0 ') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // *************** 3. vehicle query ************* $ url =" http://apis.juhe.cn/train/s "; $ Params = array (" name "=>" ", // vehicle entry name, for example, G4" key "=> $ appkey, // application APPKEY (query on the application details page) "dtype" => "", // format of the returned data, xml or json, json by default ); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // *************** 4. site arrival query ************ $ url =" http://apis.juhe.cn/train/s 2 s "; $ params = array (" start "=>" ", // departure station" end "=>" ", // terminal station" traintype "=> "", // train type, G-high-speed train K-fast T-air-conditioner express D-emu z-direct express Q-other "key" => $ appkey, // application APPKEY (query on the application details page) "dtype" => "", // format of the returned data, xml or json, json by default ); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** *********** // *************** 5.12306 real-time ticket query ********* * ** $ url =" http://apis.juhe.cn/train/yp "; $ Params = array (" key "=> $ appkey, // application APPKEY (query on the application details page)" dtype "=>" ", // format of the returned data, xml or json, the default json "from" => "", // departure site, such as Shanghai Hongqiao "to" => "", // arrival site, such: wenzhou South "date" => "", // departure date. The default value is today "tt" => "", // train count type. the default value is all, for example, G (high-speed rail), D (EMU), T (Express), Z (direct), K (fast), Q (other); $ paramstring = http_build_query ($ params ); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** *********** // *************** 6.12306 booking ①: query trains ************* $ url =" http://apis.juhe.cn/train/ticket.cc.php "; $ Params = array (" from "=>" ", // departure site name: for example, Shanghai Hongqiao" to "=>" ", // arrival site name: for example, Wenzhou South "date" => "", // The Default Day, format: "tt" => "", // train type, default all, such: G (high-speed train), D (high-speed train), T (express train), Z (direct train), K (fast train), Q (other) "key" => $ appkey, // application APPKEY (query on the application details page); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring ); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0 ') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // *************** 7. ticket sales representative query ************* $ url =" http://apis.juhe.cn/train/dsd "; $ Params = array (" province "=>" ", // province, such as: Zhejiang" city "=>" ", // city, such: wenzhou "county" => "", // district/county, such as: Lucheng district "key" => $ appkey, // app APPKEY (application details page query) "dtype" => "", // returned data format, xml or json, default json); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0 ') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // *************** 8. train station list ************ $ url =" http://apis.juhe.cn/train/s Tation. list. php "; $ params = array (" key "=> $ appkey, // application APPKEY (application details page query)" dtype "=> "", // the format of the returned data, xml or json, json by default); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring ); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0 ') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //***************************** * *******************/***** Request interface returned content * @ param string $ url [requested URL address] * @ param string $ params [request parameters] * @ param int $ ipost [whether to use the POST form] * @ return string */function juhecurl ($ url, $ params = false, $ ispost = 0) {$ httpInfo = array (); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt ($ ch, CURLOPT_USERAGENT, 'juhedata'); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt ($ ch, CURLOPT_TIMEOUT, 60); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true); if ($ ispost) {curl_setopt ($ ch, CURLOPT_POST, true); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ params); curl_setopt ($ ch, CURLOPT_URL, $ url );} else {if ($ params) {curl_setopt ($ ch, CURLOPT_URL, $ url. '? '. $ Params);} else {curl_setopt ($ ch, CURLOPT_URL, $ url) ;}}$ response = curl_exec ($ ch); if ($ response === FALSE) {// echo "cURL Error :". curl_error ($ ch); return false;} $ httpCode = curl_getinfo ($ ch, CURLINFO_HTTP_CODE); $ httpInfo = array_merge ($ httpInfo, curl_getinfo ($ ch )); curl_close ($ ch); return $ response ;}