php調(diào)用駕照題庫的方法:1、申請駕照題庫API接口;2、配置申請的appkey;3、分析接口數(shù)據(jù)并選擇考試科目類型及駕照類型;4、通過“function juhecurl($url,$params=false,$ispost=0){…}”方式請求接口返回內(nèi)容即可。
php入門到就業(yè)線上直播課:進入學(xué)習(xí)
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點擊使用
本教程操作環(huán)境:windows7系統(tǒng)、PHP8.1版、Dell G3電腦。
php怎么調(diào)用駕照題庫?
基于php的駕照題庫接口調(diào)用代碼實例
駕照題庫API接口申請:https://www.juhe.cn/docs/api/id/183?s=cpphpcn
接口說明:
-
公安部駕照考試題庫;
-
考題種類齊全,分類明確;
-
考題可按順序或者隨機兩種方試獲取
根據(jù)輸入?yún)?shù)返回相關(guān)題目。
PHP示例:
// +---------------------------------------------------------------------- //---------------------------------- // 駕照題庫調(diào)用示例代碼 - 聚合數(shù)據(jù) // 在線接口文檔:https://www.juhe.cn/docs/api/id/183?s=cpphpcn //---------------------------------- header('Content-type:text/html;charset=utf-8'); //配置您申請的appkey $appkey = "*********************"; //************1.題庫接口************ $url = "http://api2.juheapi.com/jztk/query"; $params = array( "key" => $appkey,//您申請的appKey "subject" => "",//選擇考試科目類型,1:科目1;4:科目4 "model" => "",//駕照類型,可選擇參數(shù)為:c1,c2,a1,a2,b1,b2;當subject=4時可省略 "testType" => "",//測試類型,rand:隨機測試(隨機100個題目),order:順序測試(所選科目全部題目) ); $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 "請求失敗"; } //************************************************** //************2.answer字段對應(yīng)答案************ $url = "http://api2.juheapi.com/jztk/answers"; $params = array( "key" => $appkey,//您申請的appk ); $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 "請求失敗"; } //************************************************** /** * 請求接口返回內(nèi)容 * @param string $url [請求的URL地址] * @param string $params [請求的參數(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視頻教程》