溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php怎么查詢郵編

發(fā)布時間:2022-10-24 16:39:19 來源:億速云 閱讀:104 作者:iii 欄目:編程語言

這篇文章主要講解了“php怎么查詢郵編”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“php怎么查詢郵編”吧!

php查詢郵編的方法:1、申請郵編查詢接口;2、配置申請的appkey;3、應(yīng)用appkey,并應(yīng)用詳細(xì)頁查詢;4、通過“function juhecurl($url,$params=false,$ispost=0){...}”方式請求接口返回內(nèi)容即可。

php查詢郵編

郵編查詢接口地址:https://www.juhe.cn/docs/api/id/66?s=cpphpcn

接口說明:全國30多個省市縣的郵編號碼查詢,數(shù)據(jù)權(quán)威準(zhǔn)確,數(shù)百萬條數(shù)據(jù),精確到區(qū)、縣、街道。支持按模糊地址、指定區(qū)域地址查詢郵編。

代碼示例:

// +----------------------------------------------------------------------
//----------------------------------
// 郵編查詢調(diào)用示例代碼 - 聚合數(shù)據(jù)
// 在線接口文檔:https://www.juhe.cn/docs/api/id/66?s=cpphpcn
//----------------------------------
header('Content-type:text/html;charset=utf-8');
//配置您申請的appkey
$appkey = "*********************";
//************1.郵編查詢地名************
$url = "http://v.juhe.cn/postcode/query";
$params = array(
"postcode" => "",//郵編,如:215001
"key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
"page" => "",//頁數(shù),默認(rèn)1
"pagesize" => "",//每頁返回,默認(rèn):20,最大不超過50
"dtype" => "",//返回數(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 "請求失敗";
}
//**************************************************
//************2.省份城市區(qū)域列表************
$url = "http://v.juhe.cn/postcode/pcd";
$params = array(
"key" => $appkey,//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
"dtype" => "",//返回數(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 "請求失敗";
}
//**************************************************
//************3.地名查詢郵編************
$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ì)頁查詢)
"dtype" => "",//返回數(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 "請求失敗";
}
//**************************************************
/**
* 請求接口返回內(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;
}

感謝各位的閱讀,以上就是“php怎么查詢郵編”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對php怎么查詢郵編這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI