php查詢(xún)郵編的方法:1、申請(qǐng)郵編查詢(xún)接口;2、配置申請(qǐng)的appkey;3、應(yīng)用appkey,并應(yīng)用詳細(xì)頁(yè)查詢(xún);4、通過(guò)“function juhecurl($url,$params=false,$ispost=0){…}”方式請(qǐng)求接口返回內(nèi)容即可。
php入門(mén)到就業(yè)線(xiàn)上直播課:進(jìn)入學(xué)習(xí)
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點(diǎn)擊使用
本教程操作環(huán)境:windows7系統(tǒng)、PHP8.1版、Dell G3電腦。
php查詢(xún)郵編
郵編查詢(xún)接口地址:https://www.juhe.cn/docs/api/id/66?s=cpphpcn
接口說(shuō)明:全國(guó)30多個(gè)省市縣的郵編號(hào)碼查詢(xún),數(shù)據(jù)權(quán)威準(zhǔn)確,數(shù)百萬(wàn)條數(shù)據(jù),精確到區(qū)、縣、街道。支持按模糊地址、指定區(qū)域地址查詢(xún)郵編。
代碼示例:
// +---------------------------------------------------------------------- //---------------------------------- // 郵編查詢(xún)調(diào)用示例代碼 - 聚合數(shù)據(jù) // 在線(xiàn)接口文檔:https://www.juhe.cn/docs/api/id/66?s=cpphpcn //---------------------------------- header('Content-type:text/html;charset=utf-8'); //配置您申請(qǐng)的appkey $appkey = "*********************"; //************1.郵編查詢(xún)地名************ $url = "http://v.juhe.cn/postcode/query"; $params = array( "postcode" => "",//郵編,如:215001 "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢(xún)) "page" => "",//頁(yè)數(shù),默認(rèn)1 "pagesize" => "",//每頁(yè)返回,默認(rèn):20,最大不超過(guò)50 "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.省份城市區(qū)域列表************ $url = "http://v.juhe.cn/postcode/pcd"; $params = array( "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢(xún)) "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)求失敗"; } //************************************************** //************3.地名查詢(xún)郵編************ $url = "http://v.juhe.cn/postcode/search"; $params = array( "pid" => "",//省份ID "cid" => "",//城市ID "did" => "",//區(qū)域ID "q" => "",//地名關(guān)鍵字,如:木瀆 "key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢(xún)) "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視頻教程》