文書更新:2019年06月27日(木) 午前12時58分19秒

Home > その他 > 郵便番号 > php の利用 > IW3 PROJECT の API を使用( 173 )

はじめに

  1. IW3 PROJECTのAPIを使用して作ってみました。
  2. よみがなを取得することができません。
  3. 例:東京都墨田区押上
  4. 「http://api.thni.net/jzip/131/0045.json」の情報
    ZipSearchValue({ "state":"13", "stateName":"東京都", "city":"墨田区", "street":"押上" })
  5. クラスを利用してプログラムしてあり、移植も簡単だとおもいます。
  6. ZipCode クラスには汎用性を持たすため、かな情報もいれてあります。

郵便番号から住所を検索

郵便番号:  ※ハイフォンなしで入力してください。
都道府県:
市区町村:
町域:

classの本体

<?php
class ZipCode{
	private $pref;
	private $city;
	private $town;
	private $pref_kana;
	private $city_kana;
	private $town_kana;
	private $office;
	private $office_kana;
	private $zip_code;	//ハイフォン付きの書式
	private $status;
	private $error_msg;
	function __construct($zip){	//ハイフォンなしで引数をわたす
		if(empty($zip)){
			$this->status=false;
			$this->error_msg="郵便番号が入力されていません。";
		}else if(preg_match("/^\d{7}$/",$zip)){
			$this->zip_code=preg_replace("/^([0-9]{3})([0-9]{4})$/","$1-$2",$zip);
			$this->status=true;
		}else if(preg_match("/^([0-9]{3})-([0-9]{4})$/",$zip)){
			$this->zip_code=$zip;
			$this->status=true;
		}else{
			$this->status=false;
			$this->error_msg="郵便番号 [ {$zip} ] が正しくありません。";
		}
		if($this->status){	//郵便番号の書式が正しい時
			if(strpos($zip,"-")>0){
				$code=str_replace("-","",$zip);
			}else{
				$code=$zip;
			}
			$code=preg_replace("/^([0-9]{3})([0-9]{4})$/","$1/$2.json",$code);
			$data = file_get_contents("http://api.thni.net/jzip/{$code}");	//IW3 PROJECTから情報を取得
			$data=preg_replace("/^ZipSearchValue\((.*?)\)$/","$1",$data);
			$data=json_decode($data,true);
			//var_dump($data);
			if($data!=false){	//該当する郵便番号が存在するとき
				$this->pref=$data['stateName'];
				$this->city=$data['city'];
				$this->town=$data['street'];
			}else{	//該当する郵便番号が存在しないとき
				$this->status=false;
				$this->error_msg="該当する郵便番号が存在しません。";
			}
		}
	}
	function __destruct(){
	}
	function GetPref(){
		return $this->pref;
	}
	function GetPrefKana(){
		return $this->pref_kana;
	}
	function GetCity(){
		return $this->city;
	}
	function GetCityKana(){
		return $this->city_kana;
	}
	function GetTown(){
		return $this->town;
	}
	function GetTownKana(){
		return $this->town_kana;
	}
	function GetOffice(){
		return $this->office;
	}
	function GetOfficeKana(){
		return $this->office_kana;
	}
	function GetListAll(){
		return array($this->pref,$this->pref_kana,$this->city,$this->city_kana,$this->town,$this->town_kana);
	}
	function GetList(){
		return array($this->pref,$this->city,$this->town);
	}
	function GetListKana(){
		return array($this->pref_kana,$this->city_kana,$this->town_kana);
	}
	function GetStatus(){
		return $this->status;
	}
	function GetZipCode(){
		return $this->zip_code;
	}
	function GetErrMsg(){
		return $this->error_msg;
	}
}
?>

htmlの表示部

<?php
$err="";
if(isset($_POST["search"])){	//住所検索ボタンが押された時
	$zip=$_POST["zip"];
	$obj=new ZipCode($zip);
	if(!$obj->GetStatus()){
		$err="{$obj->GetErrMsg()}";
	}
	list($pref,$city,$town)=$obj->GetList();
}else{
	$zip="";
	$pref="";
	$city="";
	$town="";
}
$ret=<<<DOE
	<form method="post" action="{$_SERVER["REQUEST_URI"]}">
		<table>
			<tr><th>郵便番号:</th><td><input type="text" name="zip" size="8" maxlength="7" value="{$zip}"><input type="submit" name="search" value="住所検索"> ※ハイフォンなしで入力してください。{$err}</td></tr>
			<tr><th>都道府県:</th><td><input type="text" name="pref" size="8" value="{$pref}"></td></tr>
			<tr><th>市区町村:</th><td><input type="text" name="city" size="30" value="{$city}"></td></tr>
			<tr><th>町域:</th><td><input type="text" name="town" size="30" value="{$town}"></td></tr>
		</table>
	</form>
DOE;
	echo $ret;
?>