php識(shí)別火車票的方法:1、開(kāi)通火車票識(shí)別API接口;2、創(chuàng)建一個(gè)PHP示例文件;3、設(shè)置“header('Content-type:text/html;charset=utf-8');”;4、配置申請(qǐng)的appkey;5、應(yīng)用appkey;6、請(qǐng)求URL;7、獲取接口數(shù)據(jù)并分析處理即可。
php入門到就業(yè)線上直播課:進(jìn)入學(xué)習(xí)
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點(diǎn)擊使用
本教程操作環(huán)境:windows7系統(tǒng)、PHP8.1版、Dell G3電腦。
php怎么識(shí)別火車票?
1、開(kāi)通火車票識(shí)別API接口:
通過(guò)https://www.juhe.cn/docs/api/id/522?s=cpphpcn
注冊(cè)及開(kāi)通
接口說(shuō)明:
-
該接口支持火車票全字段的識(shí)別,包括編號(hào)、票價(jià)、姓名、座位號(hào)、出發(fā)時(shí)間、出發(fā)站、到達(dá)站、車次、席別等。
-
返回格式:json
-
請(qǐng)求方式:post
-
請(qǐng)求示例:http://v.juhe.cn/trainTicketOcr/index.php
2、基于php的12306火車票查詢接口調(diào)用代碼實(shí)例
php代碼示例如下:
1.[代碼][PHP]代碼 // +------------------- header('Content-type:text/html;charset=utf-8'); //配置您申請(qǐng)的appkey $appkey = "*********************"; //************1.站到站查詢(含票價(jià))************ $url = "http://apis.juhe.cn/train/s2swithprice"; $params = array( "start" => "",//出發(fā)站 "end" => "",//終點(diǎn)站 "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢) "dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)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 "請(qǐng)求失敗"; } //************************************************** //************2.12306訂票②:車次票價(jià)查詢************ $url = "http://apis.juhe.cn/train/ticket.price.php"; $params = array( "train_no" => "",//列次編號(hào),對(duì)應(yīng)12306訂票①:查詢車次中返回的train_no "from_station_no" => "",//出發(fā)站序號(hào),對(duì)應(yīng)12306訂票①:查詢車次中返回的from_station_no "to_station_no" => "",//出發(fā)站序號(hào),對(duì)應(yīng)12306訂票①:查詢車次中返回的to_station_no "date" => "",//默認(rèn)當(dāng)天,格式:2014-12-25 "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢) ); $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 "請(qǐng)求失敗"; } //************************************************** //************3.車次查詢************ $url = "http://apis.juhe.cn/train/s"; $params = array( "name" => "",//車次名稱,如:G4 "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢) "dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)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 "請(qǐng)求失敗"; } //************************************************** //************4.站到站查詢************ $url = "http://apis.juhe.cn/train/s2s"; $params = array( "start" => "",//出發(fā)站 "end" => "",//終點(diǎn)站 "traintype" => "",//列車類型,G-高速動(dòng)車 K-快速 T-空調(diào)特快 D-動(dòng)車組 Z-直達(dá)特快 Q-其他 "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢) "dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)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 "請(qǐng)求失敗"; } //************************************************** //************5.12306實(shí)時(shí)余票查詢************ $url = "http://apis.juhe.cn/train/yp"; $params = array( "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢) "dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)json "from" => "",//出發(fā)站,如:上海虹橋 "to" => "",// 到達(dá)站,如:溫州南 "date" => "",//出發(fā)日期,默認(rèn)今日 "tt" => "",//車次類型,默認(rèn)全部,如:G(高鐵)、D(動(dòng)車)、T(特快)、Z(直達(dá))、K(快速)、Q(其他) ); $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 "請(qǐng)求失敗"; } //************************************************** //************6.12306訂票①:查詢車次************ $url = "http://apis.juhe.cn/train/ticket.cc.php"; $params = array( "from" => "",//出發(fā)站名稱:如上海虹橋 "to" => "",//到達(dá)站名稱:如溫州南 "date" => "",//默認(rèn)當(dāng)天,格式:2014-07-11 "tt" => "",//車次類型,默認(rèn)全部,如:G(高鐵)、D(動(dòng)車)、T(特快)、Z(直達(dá))、K(快速)、Q(其他) "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢) ); $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 "請(qǐng)求失敗"; } //************************************************** //************7.火車票代售點(diǎn)查詢************ $url = "http://apis.juhe.cn/train/dsd"; $params = array( "province" => "",//省份,如:浙江 "city" => "",//城市,如:溫州 "county" => "",//區(qū)/縣,如:鹿城區(qū) "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢) "dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)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 "請(qǐng)求失敗"; } //************************************************** //************8.列車站點(diǎn)列表************ $url = "http://apis.juhe.cn/train/station.list.php"; $params = array( "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢) "dtype" => "",//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)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 "請(qǐng)求失敗"; } //************************************************** /** * 請(qǐng)求接口返回內(nèi)容 * @param string $url [請(qǐng)求的URL地址] * @param string $params [請(qǐng)求的參數(shù)] * @param int $ipost [是否采用POST形式] * @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; }
登錄后復(fù)制
推薦學(xué)習(xí):《PHP視頻教程》