Home > その他 > 郵便番号 > php の利用 > IW3 PROJECT の API を使用( 173 )
ZipSearchValue({ "state":"13", "stateName":"東京都", "city":"墨田区", "street":"押上" })<?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;
}
}
?><?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;
?>